0
我正在嘗試使用GridBagLayout。我需要一個垂直和水平集中的JLabel - 這很容易,我甚至不必創建任何GridBagConstraints。我也想把JButton放在右下角,當我嘗試這樣做時,我居中的面板向左移動或按鈕向上移動。GridBagLayout問題
EXPECTING GETTING THIS OR THIS
+-----------+ +-----------+ +-----------+
| | | | | |
| | | | | |
| | | | | |
| +---+ | | +---+ | | +---+ |
| | | | | | | | | | | |
| +---+ | | +---+ | | +---++---+|
| | | | | | ||
| | | | | +---+|
| +---+ | +---+ | |
| | | | | | | |
+-------+---+ +-------+---+ +-----------+
bigPanel = new JPanel();
bigPanel.setPreferredSize(new Dimension(320, 640));
bigPanel.setLayout(new GridBagLayout());
label = new JLabel();
label.setPreferredSize(new Dimension(100,95));
button = new JButton();
button.setPreferredSize(new Dimension(100,25));
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER;
bigPanel.add(label, c);
c.anchor = GridBagConstraints.LAST_LINE_END;
bigPanel.add(button, c);
我還試圖用它在這裏描述http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html其他方面的限制,但每一次出現問題。