gpt4 book ai didi

java - jlabel/按钮格式问题

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

类别标签 1、2、3、4 和 5 似乎与我分配给它们的行不一致。我不明白问题出在哪里; x 值相等。如果有人能引导我走向正确的方向,那就太好了。

这是我的代码

public class GovernmentJepordyGame {

public static void main(String[] args)
{
JFrame frame = new JFrame("Jepordy");
frame.setVisible(true);
frame.setSize(1300,900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

frame.getContentPane().add(panel, BorderLayout.PAGE_START);
/*JLabel label = new JLabel("Welcome to government jepordy.");*/
JLabel catagoryOne = new JLabel("Catagory 1");
c.gridx = 0;
c.gridy = 0;
panel.add(catagoryOne, c);
JButton button500A = new JButton("500");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500A, c);
JButton button400A = new JButton("400");
c.gridx = 0;
c.gridy = 2;
panel.add(button400A, c);
JButton button300A = new JButton("300");
c.gridx = 0;
c.gridy = 3;
panel.add(button300A, c);
JButton button200A = new JButton("200");
c.gridx = 0;
c.gridy = 4;
panel.add(button200A, c);
JButton button100A = new JButton("100");
c.gridx = 0;
c.gridy = 5;
panel.add(button100A, c);

JLabel catagoryTwo = new JLabel("Catagory 2");
c.gridx = -1;
c.gridy = 0;
panel.add(catagoryTwo, c);
JButton button500B = new JButton("500");
c.gridx = -1;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500B, c);
JButton button400B = new JButton("400");
c.gridx = -1;
c.gridy = 2;
panel.add(button400B, c);
JButton button300B = new JButton("300");
c.gridx = -1;
c.gridy = 3;
panel.add(button300B, c);
JButton button200B = new JButton("200");
c.gridx = -1;
c.gridy = 4;
panel.add(button200B, c);
JButton button100B = new JButton("100");
c.gridx = -1;
c.gridy = 5;
panel.add(button100B, c);

JLabel catagoryThree = new JLabel("Catagory 3");
c.gridx = -2;
c.gridy = 0;
panel.add(catagoryThree);
JButton button500C = new JButton("500");
c.gridx = -2;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500C, c);
JButton button400C = new JButton("400");
c.gridx = -2;
c.gridy = 2;
panel.add(button400C, c);
JButton button300C = new JButton("300");
c.gridx = -2;
c.gridy = 3;
panel.add(button300C, c);
JButton button200C = new JButton("200");
c.gridx = -2;
c.gridy = 4;
panel.add(button200C, c);
JButton button100C = new JButton("100");
c.gridx = -2;
c.gridy = 5;
panel.add(button100C, c);

JLabel catagoryFour = new JLabel("Catagory 4");
c.gridx = -3;
c.gridy = 0;
panel.add(catagoryFour);
JButton button500D = new JButton("500");
c.gridx = -3;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500D, c);
JButton button400D = new JButton("400");
c.gridx = -3;
c.gridy = 2;
panel.add(button400D, c);
JButton button300D = new JButton("300");
c.gridx = -3;
c.gridy = 3;
panel.add(button300D, c);
JButton button200D = new JButton("200");
c.gridx = -3;
c.gridy = 4;
panel.add(button200D, c);
JButton button100D = new JButton("100");
c.gridx = -3;
c.gridy = 5;
panel.add(button100D, c);

JLabel catagoryFive = new JLabel("Catagory 5");
c.gridx = -4;
c.gridy = 0;
panel.add(catagoryFive);
JButton button500E = new JButton("500");
c.gridx = -4;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500E, c);
JButton button400E = new JButton("400");
c.gridx = -4;
c.gridy = 2;
panel.add(button400E, c);
JButton button300E = new JButton("300");
c.gridx = -4;
c.gridy = 3;
panel.add(button300E, c);
JButton button200E = new JButton("200");
c.gridx = -4;
c.gridy = 4;
panel.add(button200E, c);
JButton button100E = new JButton("100");
c.gridx = -4;
c.gridy = 5;
panel.add(button100E, c);
}
}

最佳答案

gridx 应该是一个正值,例如...

    JFrame frame = new JFrame("Jepordy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

frame.getContentPane().add(panel, BorderLayout.PAGE_START);

c.gridx = 0;

for (int index = 0; index < 5; index++) {

c.gridy = 0;
JLabel label = new JLabel("Catagory " + index);
c.insets = new Insets(0, 0, 0, 0);
panel.add(label, c);
c.insets = new Insets(65, 65, 65, 65);
for (int cat = 1; cat < 6; cat++) {
c.gridy++;
panel.add(new JButton(Integer.toString(cat * 100)), c);
}

c.gridx++;
}
frame.setVisible(true);
frame.setSize(1300, 900);

例如...

关于java - jlabel/按钮格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351124/

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