嗨我想添加2 JPanel的JFrame採取JFrame.I的全部寬度和高度設法添加它們與GridBagLayout()但我似乎無法設置。使用的setSize()的JPanels的大小,我也tryied所用ipady和ipadx,而一開始就有些工作我交鋒一些按鈕的整個佈局成爲mess.Here後是我的代碼:Java 2 JPanel的在一個JFrame佈局
JFrame tradeframe = new JFrame("Trade");
JPanel P1panel = new JPanel();
P1panel.setBackground(Color.red);
JPanel P2panel = new JPanel();
P2panel.setBackground(Color.BLACK);
tradeframe.setVisible(true);
tradeframe.setSize(600, 400);
tradeframe.setResizable(false);
tradeframe.setLocationRelativeTo(null);
tradeframe.setLayout(new GridBagLayout());
P1panel.add(new JButton ("P1 Agree"));
P2panel.add(new JButton ("P2 Agree"));
GridBagConstraints a = new GridBagConstraints();
a.gridx = 0;
a.gridy = 0;
a.weightx = 360;
a.weighty = 300;
//a.fill = GridBagConstraints.HORIZONTAL;
tradeframe.add(P1panel , a);
GridBagConstraints b = new GridBagConstraints();
b.gridx = 1;
b.gridy = 0;
b.weightx = 360;
b.weighty = 300;
// b.fill = GridBagConstraints.HORIZONTAL;
tradeframe.add(P2panel , b);
我怎樣才能讓每個JPanel的寬度是300px,高度是400px?
當您使用LayoutManager時,您不使用setXXXSize(),這是LayoutManager的工作來擔心這一點。再次閱讀GridBagLayout教程,看看他們告訴你什麼值爲weightx和weighty :-) – 2012-03-12 13:33:46