所以我在屏幕上有2个面板。一个已设置为WEST,另一个设置为CENTER以占用屏幕的其余部分。 我遇到的问题:
public MainPanel() {
super();
this.setLayout(new BorderLayout());
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 1));
buttonPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.add(buttonPanel, BorderLayout.WEST);
centerPanel = new JPanel();
centerPanel.setBackground(Color.black);
centerPanel.setOpaque(true);
centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.add(centerPanel, BorderLayout.CENTER);
JButton options = new JButton("Options");
//options.setPreferredSize(new Dimension(100, 100));
buttonPanel.add(options);
options.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RichardDemo.this.switchPanel(MainPanel.this);
}
});
JButton start = new JButton("Start Game");
buttonPanel.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
}
答案 0 :(得分:1)
当我改变按钮的大小时,它既可以改变面板的大小,也可以什么都不做?
您无需更改组件的大小。只需将其留给布局管理器即可调整组件大小。
对于调整大小,2个按钮总是会做同样的事情吗?
不需要。这取决于您正在使用的布局。
有没有办法让不同的按钮具有不同的尺寸,中间有不同的空间而不必将它们分配给不同的面板?
是的,您可以使用 GridBagLayout 来实现它。