我创建了一个JFrame并将JPanel设置为其内容面板,我在该容器中添加了大约30个按钮,但JFrame向我显示了一行按钮,它不允许我扩展高度。
public class UIDemo{
private JPanel contentPanel = new JPanel();
private JButton buttons[] = new JButton[30];
public UIDemo(JFrame frame){
for(int i = 0; i < buttons.length; i++){
buttons[i] = new JButton("Button"+Integer.toString(i));
contentPanel.add(buttons[i]);
}//Add all buttons into content panel.
frame.setContentPane(contentPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new UIDemo(new JFrame());
}
});
}
}
答案 0 :(得分:2)
JPanel
已在API
FlowLayout
FlowLayout
仅接受PreferredSize
,JComponents
中的JPanel
无法调整大小
查看GridLayout
,
其他选项包括GridBagLayout
或今日自定义MigLayout