gpt4 book ai didi

java - GridBagLayout 故障排除

转载 作者:行者123 更新时间:2023-12-01 18:13:15 25 4
gpt4 key购买 nike

所以我决定让我的 GUI 看起来更好一点,并使用 GridBagLayout 来代替。以下是我添加到面板中的对象:

choosePanel = new JPanel();
choosePanel.setLayout(new GridBagLayout());

chooseLabel = new JLabel("Choose a quarter to input data for:");
addItem(chooseLabel, 0, 0, 1, 1);

qGroup = new ButtonGroup();

q1 = new JRadioButton("Q1");
qGroup.add(q1);
q1.setSelected(true);
addItem(chooseLabel, 0, 1, 1, 1);

q2 = new JRadioButton("Q2");
qGroup.add(q2);
addItem(chooseLabel, 0, 2, 1, 1);

q3 = new JRadioButton("Q3");
qGroup.add(q3);
addItem(chooseLabel, 0, 3, 1, 1);

q4 = new JRadioButton("Q4");
qGroup.add(q4);
addItem(chooseLabel, 0, 4, 1, 1);

chooseButton = new JButton("Press to Enter Quarter");
chooseButton.addActionListener(e->{
cl.show(mainPanel, "Info");
this.setSize(330, 240);
});
chooseButton.setPreferredSize(new Dimension(200, 100));
addItem(chooseLabel, 1, 1, 1, 1);

resetButton = new JButton("Reset all previous data");
resetButton.addActionListener(e->{

});
resetButton.setPreferredSize(new Dimension(200, 100));
addItem(chooseLabel, 1, 2, 1, 1);

这是“addItem”方法:

private void addItem(JComponent c, int x, int y, int width, int height){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = 100.0;
gbc.weighty = 100.0;
gbc.fill = GridBagConstraints.NONE;
choosePanel.add(c, gbc);
}

我的问题是,当我运行程序时,显示的只是屏幕中间的选择标签,没有其他内容。有人知道怎么修这个东西吗?......

最佳答案

改变

q1 = new JRadioButton("Q1");
qGroup.add(q1);
q1.setSelected(true);
addItem(chooseLabel, 0, 1, 1, 1);

q2 = new JRadioButton("Q2");
qGroup.add(q2);
addItem(chooseLabel, 0, 2, 1, 1);

q3 = new JRadioButton("Q3");
qGroup.add(q3);
addItem(chooseLabel, 0, 3, 1, 1);

q4 = new JRadioButton("Q4");
qGroup.add(q4);
addItem(chooseLabel, 0, 4, 1, 1);

q1 = new JRadioButton("Q1");
qGroup.add(q1);
q1.setSelected(true);
addItem(q1 , 0, 1, 1, 1);

q2 = new JRadioButton("Q2");
qGroup.add(q2);
addItem(q2, 0, 2, 1, 1);

q3 = new JRadioButton("Q3");
qGroup.add(q3);
addItem(q3, 0, 3, 1, 1);

q4 = new JRadioButton("Q4");
qGroup.add(q4);
addItem(q4, 0, 4, 1, 1);

等等..您一直在添加chooseLabel

关于java - GridBagLayout 故障排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com