我想让我的按钮填满面板的宽度。这是我的代码,创建了一个按钮和一个面板,然后将按钮添加到面板上。
buttonPane = makeButtonPanel();
button = makeButton("Hello");
button2 = makeButton("World");
buttonPane.add(button);
buttonPane.add(button2);
private JButton makeButton(String msg){
JButton button = new JButton(msg);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
return button;
}
private JPanel makeButtonPanel(){
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
btnPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return btnPanel;
}
这是我得到的输出...