如何將按鈕放在一起並排放置。我使用了gridBagLayout來設計佈局。問題在於按鈕距離彼此太遠。我試圖選擇CENTER作爲錨點,但這會使按鈕重疊。如果我使用WEST和EAST,則按鈕位置彼此相距太遠。並排放置按鈕
例如保存----------新模式,而不是保存NEW_PATTERN。
JButton bSave = new JButton("SAVE");
JButton bPattern = new JButton("NEW_PATTERN");
con = new GridBagConstraints();
con.anchor=GridBagConstraints.WEST;
con.gridy = 3; con.gridx = 0;
con.gridwidth = 1; con.gridheight = 1;
con.insets= new Insets(2,5,2,2);
m.setConstraints(bSave, con);
c.add(bSave);
con.weightx=1;
con.gridy=3; con.gridx=0;
con.anchor=GridBagConstraints.EAST;
m.setConstraints(bPattern,con);
c.add(bPattern);
是否有可能同時具有gridBagLayout和flowLayout? – Jessy 2009-10-08 02:08:02
是和否:)一個'Container'只能有一個'LayoutManager'。但是,在GridBagLayout中,您可以添加'JPanels',這些'JPanel''可以使用他們自己的'LayoutManager',因此您可以在其中一個面板中使用'FlowLayout'。 – akf 2009-10-08 02:28:16
謝謝akf,我已經用你的代碼解決了這個問題:-) ... – Jessy 2009-10-08 02:35:15