起初,我想对我的英语不好对不起,但我会尽力而为。我想使用GridBackLayout Manager。它以JFrame开头。 GUI应该具有三行。我想使用GridBagLayout Manager找到五个JPanel。第一行应该是整个宽度上的一个JPanel。在第二行中,两个JPanel。整个宽度的第一个0.25和第二个0.75。在第三行中,也应该有两个JPanels,但是两者都占整个宽度的0.5。问题从第三行开始。不能说两者都应为整个宽度的0.5。由于第三行,第二行也是错误的。我不知道我做错了什么。我希望有人能帮助我。
public class GridBagGUI {
public GridBagGUI() {
createGUI();
}
private void createGUI() {
final JFrame frame= new JFrame("GridBagTest");
frame.setPreferredSize(new Dimension(800, 600));
final GridBagLayout gbl = new GridBagLayout();
final GridBagConstraints gbc=new GridBagConstraints();
final JMenuBar menuBar=new JMenuBar();
final JMenu menu=new JMenu("test");
final JMenuItem menuitem = new JMenuItem("test");
menu.add(menuitem);
menuBar.add(menu);
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill=GridBagConstraints.BOTH;
final JPanel panel1 = new JPanel();
panel1.setBorder(new LineBorder(Color.BLACK));
gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=4;
gbc.gridheight=1;
gbc.weightx=1.0;
gbc.weighty=0.3;
gbl.setConstraints(panel1, gbc);
frame.add(panel1);
final JPanel panel2 = new JPanel();
panel2.setBorder(new LineBorder(Color.BLACK));
gbc.gridx=0;
gbc.gridy=1;
gbc.gridwidth=1;
gbc.gridheight=1;
gbc.weightx=0.25;
gbc.weighty=0.3;
gbl.setConstraints(panel2, gbc);
frame.add(panel2);
final JPanel panel3 = new JPanel();
panel3.setBorder(new LineBorder(Color.BLACK));
gbc.gridx=1;
gbc.gridy=1;
gbc.gridwidth=3;
gbc.gridheight=1;
gbc.weightx=0.75;
gbc.weighty=0.3;
gbl.setConstraints(panel3, gbc);
frame.add(panel3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menuBar);
frame.setLayout(gbl);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}