a layout question (swing classes) --> (I' m learning ...)
Hi,
I can' t understand how to handle the layouts with the swing classes.
I need to be able to put different objects (JButton, JLabel, ...) on different locations on an interface, and I can' t understand how it is done.
I have I code example that I understand. Can somebody give me a better version where the layout has changed.
It looks lik this:
BUTTON
BUTTON
TEXTFIELD
I would like to have:
BUTTON LABEL TEXTFIELD
BUTTON
BUTTON
BUTTON
BUTTON
Thanks for looking at my problem ....
this is the code that I have:
Code:
//v 1.3
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingApplication {
public Component createOtherComponents() {
final JTextField textField = new JTextField(10);
JButton Addbutton = new JButton("ADD");
JButton DeleteButton = new JButton("DELETE");
Addbutton.setMnemonic(KeyEvent.VK_I);
Addbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText() + "test" + "\n");
}
});
DeleteButton.setMnemonic(KeyEvent.VK_I);
DeleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
}
});
JPanel pane = new JPanel();
pane.setBorder(BorderFactory.createEtchedBorder() //right
);
pane.setLayout(new GridLayout(0, 1));
pane.add(Addbutton);
pane.add(DeleteButton);
pane.add(textField);
return pane;
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
JFrame frame = new JFrame("TestApplication");
SwingApplication app = new SwingApplication();
Component Othercontents = app.createOtherComponents();
frame.getContentPane().add(Othercontents);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}