最近,我已經開始在Java中進行UI開發;我曾經在WPF中進行UI開發。關於Java做事方式的一些事情令人困惑。使用BoxLayout在JPanel內設置JButton的大小不能按預期工作
我想要實現的是設置一個按鈕的最小尺寸。 下面是簡化的代碼:
public class MainGameView extends JPanel {
public MainGameView(DefaultController controller) {
this.controller = controller;
CreateUI();
}
private void CreateUI() {
MenuPanel = new javax.swing.JPanel();
StartGameBtn = new JButton("Start Game");
// Creating menu
MenuPanel.setLayout(new BoxLayout(MenuPanel, BoxLayout.Y_AXIS));
MenuPanel.setPreferredSize(new Dimension(200, 200));
StartGameBtn.setAlignmentX(Component.LEFT_ALIGNMENT);
StartGameBtn.setMinimumSize(new Dimension(200, 30));
MenuPanel.add(StartGameBtn);
}
}
所以我的理解,如果容器被分配200個像素的寬度,就應該相應地分配寬度爲200個像素的按鈕。但是按鈕保持相同的大小。我在這裏錯過了什麼嗎?