我想要一个 3x3 的网格布局,结构如下:
_ _ _ _ _ _ _ _ _ _ _ _
| Label |
| Button Button Button |
| TextArea |
|_ _ _ _ _ _ _ _ _ _ _ |
但是布局被破坏了。
public static void main(String[] args){
JFrame frame = new JFrame("title");
frame.setSize(400,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(3,3);
JPanel pan0 = new JPanel();
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
pan0.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
JLabel title = new JLabel("title");
title.setFont(new Font("Serif", Font.BOLD, 40));
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
pan0.add(title, c);
JButton b1 = new JButton("button1");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
pan0.add(b1, c);
JButton b2 = new JButton("button2");
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
pan0.add(b2, c);
JButton b3 = new JButton("button3");
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 1;
pan0.add(b3, c);
TextArea text1 = new TextArea(15,40);
c.gridx = 2;
c.gridy = 0;
c.gridwidth = 3;
pan0.add(text1, c);
frame.add(pan0);
frame.pack();
}
}