类别标签1,2,3,4和5似乎与第I行对齐,而不是分配给它们。我不明白问题所在; 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);
}
}
答案 0 :(得分:1)
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);
例如......
答案 1 :(得分:0)
你得x和y混淆了。屏幕的左上角是0,0。当你向右移动时,x增加,当你向下移动时,y增加。因此,不需要为x坐标系指定( - )值,它可能会开始移动屏幕左侧的按钮。每个按钮,尝试增加x网格值,而不是y,你应该得到你想要的结果。
gridx = 10 .... gridx = 20 ..依此类推。
更好的是,实现某种循环来迭代每个按钮并在每次迭代时增加x值。
或者,查看flowlayout经理,他们会自动处理。