我是用Java创建GUI的新手。我试图使用GridBagLayout的一个项目,但我被困在这里。
我想让按钮和两个JLabel的大小相同,但按钮更大。
public class Menu extends JPanel {
public Menu() {
this.setBackground(Color.black);
GridBagLayout gb = new GridBagLayout();
setLayout(gb);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
JButton b1 = new JButton();
b1.setText("Start");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty= 1;
gb.setConstraints(b1, gbc);
add(b1);
JPanel Gap = new JPanel();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty= 1;
gb.setConstraints(Gap, gbc);
add(Gap);
RadioPanel radio = new RadioPanel();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gb.setConstraints(radio, gbc);
add(radio);
}
}
Click here to see my GUI
这是我的代码,它看起来是这样的。
1条答案
按热度按时间s4n0splo1#
GridLayout
,不是GridBagLayout
对于三个大小相等的小部件,使用
GridLayout
而不是GridBagLayout
。请参见Oracle教程How to Use GridLayout。下面是一个带有两个标签的按钮的示例,所有标签的大小都相同。