Re: [問題] 一個 GridBagLayout 的問題

看板java作者 (非天夜翔)時間16年前 (2009/07/05 02:13), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串6/6 (看更多)
constraints.fill = GridBagConstraints.BOTH .VERTICAL .HORIZONTAL constraints.weightx .weighty 在 addComponent 之前作修改, 會有不同的效果。 例如: constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 1.0; constraints.weighty = 1.0; 加在 addComponent(button1); 之前 且其他 constraints.fill 刪掉 就會得到 ┌───────────────────────┐ │gridbaglayout              _□╳│ ├───────────────────────┤ │ │Button1 Button2 option1▼ option1▼ 口filled│ │ │ ├───────────────────────┤ │ │ │ │ │ │ ├───────────────────────┤ │ │ │textfield │ │ │ └───────────────────────┘ ※ 引述《Liszt04 (走路往下看都會怕)》之銘言: : 請問依下依個排版的問題 : 我希望排出這樣的排版 : ┌┬┬┬┬┐ : ├┴┴┴┴┤ : ├────┤ : └────┘ : 上面是五個button, 中間是依個Jpanel可以畫畫, 下面是一個JTextField : code如下: : import javax.swing.*; : import java.awt.*; : public class GridBag extends JFrame : { : private GridBagLayout layout; : private GridBagConstraints constraints; : private JButton button1; : private JButton button2; : private JComboBox combobox1; : private JComboBox combobox2; : private JCheckBox checkbox; : private String list[] = {"option1", "option2", "option3", "option4"}; : private JPanel panel; : private JTextField textarea; : public GridBag() : { : super("gridbaglayout"); : layout = new GridBagLayout(); : setLayout(layout); : constraints = new GridBagConstraints(); : button1 = new JButton("Button1"); : button2 = new JButton("Button2"); : combobox1 = new JComboBox(list); : combobox2 = new JComboBox(list); : checkbox = new JCheckBox("filled"); : textarea = new JTextField("textfield", 50); : constraints.fill = GridBagConstraints.HORIZONTAL; : addComponent(button1, 0, 0, 1, 1); : addComponent(button2, 1, 0, 1, 1); : addComponent(combobox1, 2, 0, 1, 1); : addComponent(combobox2, 3, 0, 1, 1); : addComponent(checkbox, 4, 0, 1, 1); : constraints.fill = GridBagConstraints.BOTH; : addComponent(panel, 0, 1, 5, 1); : constraints.fill = GridBagConstraints.HORIZONTAL; : addComponent(textarea, 0, 2, 5, 1); : } : public void addComponent(Component component, int column, int row, int : width, int height) : { : constraints.gridx = column; : constraints.gridy = row; : constraints.gridwidth = width; : constraints.gridheight = height; : layout.setConstraints(component, constraints); : add(component); : } : } : 他跑出來的結果是: : exceptionx in thread "main" lava.lang.Nullpointerexception : at java.util.Hastable.put(Hashtable.java:399) : at java.awt.GridBagLayout.setConstraints(GridBagLayout.java:482) : at GridBag.addComponent(GridBag.java:61) : at GridBag.(init)(GridBag.java:48) : at GridBagTest.main(GridBagTest.java:8) : : 這是GridBagTest的code: : mport javax.swing.*; : import java.awt.*; : public class GridBagTest : { : public static void main(String args[]) : { : GridBag g = new GridBag(); : g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); : g.setSize(600, 400); : g.setResizable(false); : g.setVisible(true); : } : } : 要是紅色部分沒有打的話 輸出是正確的 : 加上去panel之後整個跑不出來 : add(panel) 換成add(button) 的話 排版竟然也是錯的 囧 : 請問一下是我哪邊寫錯了 還是我哪邊觀念不太正確 : 麻煩好心的大大幫我看一下吧 : 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.9.2

07/05 02:30, , 1F
這 ASCII 示意圖也太精美了吧?
07/05 02:30, 1F
※ 編輯: csihcs 來自: 211.74.9.2 (07/05 02:36)

07/05 09:08, , 2F
我試試看 真是大感謝~!
07/05 09:08, 2F
文章代碼(AID): #1AJvklDQ (java)
討論串 (同標題文章)
文章代碼(AID): #1AJvklDQ (java)