Trouble with formatting Swing components
Date: September 25, 2010 10:28AM
I am creating an application which opens several different internal frames within the main frame of the application, depending on what menu item is selected. I've created a small separate application just for GUI testing purposes and here's its code:
import javax.swing.*;
import java.awt.*;
public class Test {
JFrame window = new JFrame("Test");
JDesktopPane main = new JDesktopPane();
JPanel secondary = new JPanel();
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem item = new JMenuItem("Click");
public JFrame getWindow(int w, int h) {
window.setSize(w, h);
window.setContentPane(main);
window.setJMenuBar(getMenuBar());
window.setVisible(true);
return window;
}
public JInternalFrame getIntFrame(String title, int w, int h){
JInternalFrame mini = new JInternalFrame(title, false, true, false, false);
mini.setSize(w, h);
JPanel secondary = new JPanel();
mini.setContentPane(secondary);
mini.setVisible(true);
window.getContentPane().add(mini);
return mini;
}
public JMenuBar getMenuBar() {
menubar.add(getMenu());
return menubar;
}
public JMenu getMenu(){
menu.add(getItem());
return menu;
}
public JMenuItem getItem(){
item.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JLabel label1 = new JLabel("Text:");
JLabel label2 = new JLabel("Numerical code:");
JTextField tf1 = new JTextField(12);
String[] values = {"0", "1"};
JComboBox cb2 = new JComboBox(values);
JButton btn = new JButton("Enter");
GridBagConstraints label1GB = new GridBagConstraints();
GridBagConstraints label2GB = new GridBagConstraints();
GridBagConstraints tf1GB = new GridBagConstraints();
GridBagConstraints cb2GB = new GridBagConstraints();
GridBagConstraints btnGB = new GridBagConstraints();
label1GB.gridx = 1;
label1GB.gridy = 1;
label1GB.insets = new Insets(2, 2, 2, 2);
label1.setHorizontalAlignment(JLabel.LEFT);
label2GB.gridx = 1;
label2GB.gridy = 2;
label2GB.insets = new Insets(5, 5, 5, 5);
label2.setHorizontalAlignment(JLabel.LEFT);
tf1GB.gridx = 2;
tf1GB.gridy = 1;
tf1GB.insets = new Insets(5, 5, 5, 5);
cb2GB.gridx = 2;
cb2GB.gridy = 2;
cb2GB.insets = new Insets(5, 5, 5, 5);
btnGB.gridx = 2;
btnGB.gridy = 3;
btnGB.insets = new Insets(5, 5, 5, 5);
JInternalFrame intfrm = getIntFrame("Internal", 400, 300);
intfrm.getContentPane().setLayout(new GridBagLayout());
intfrm.getContentPane().add(label1, label1GB);
intfrm.getContentPane().add(tf1, tf1GB);
intfrm.getContentPane().add(label2, label2GB);
intfrm.getContentPane().add(cb2, cb2GB);
intfrm.getContentPane().add(btn, btnGB);
}
});
return item;
}
public static void main(String[] args) {
Test test = new Test();
test.getWindow(500, 400);
}
}
My question is why are all of the elements within the internal frame centered despite the labels having their horizontal alignment set to left? Shouldn't it be possible to align them within the grid limits? And how do I go about aligning the text field, combo box and the button too?