Hi, 請問一下,
我想用 GridBagLayout 排出下面的面版:
┌────┬────┬────┐
│Button 1│Button 2│Button 3│
├────┴─┬──┴────┤
│ Button 4 │ Button 5 │
└──────┴───────┘
我使用以下程式碼:
public static void main(String[] args)
{
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3 = new JButton("Button 3");
JButton b4 = new JButton("Button 4");
JButton b5 = new JButton("Button 5");
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
panel.add(b1, c);
c.gridx = 2;
panel.add(b2, c);
c.gridx = 4;
panel.add(b3, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
panel.add(b4, c);
c.gridx = 3;
panel.add(b5, c);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(400, 400);
frame.setVisible(true);
}
結果出來變成:
┌────┬────┬────┐
│Button 1│Button 2│Button 3│
├────┼────┴────┤
│Button 4│ Button 5 │
└────┴─────────┘
我想半天仍不知我錯在哪裡,
所以想來請教板上的各位, 先謝謝了!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.132.133.199
※ 編輯: eliang 來自: 220.132.133.199 (05/04 21:44)
推
05/04 22:12, , 1F
05/04 22:12, 1F
→
05/04 22:14, , 2F
05/04 22:14, 2F
推
05/04 22:54, , 3F
05/04 22:54, 3F
→
05/04 23:08, , 4F
05/04 23:08, 4F
→
05/04 23:09, , 5F
05/04 23:09, 5F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 6 篇):