2014-12-08 103 views
0

類別標籤1,2,3,4和5似乎不與我分配給他們的行對齊。我不明白問題在哪裏; x值相等。如果有人能帶領我走向正確的方向,那將會非常棒。問題jlabel /按鈕格式化

這裏是我的代碼

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

*「.. x值相等..」* x表示列,而不是行! – 2014-12-08 04:05:16

+0

'c.gridx = -1;'不是'gridx'的有效值。 – MadProgrammer 2014-12-08 04:06:08

+0

順便說一句 - 'jepordy'拼寫爲'jeopardy'.. – 2014-12-08 04:36:39

回答

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); 

例如...

0

你得到x和y的混合向上。屏幕的左上角是0,0。隨着您向右移動,x增加,隨着您向下移動,y增加。因此,爲您的x座標系分配一個( - )值不是必需的,它可能會將您的按鈕從屏幕移動到左邊。每個按鈕,嘗試增加x的網格值,而不是y,你應該得到你想要的結果。

gridx = 10 .... gridx = 20 ..等等。

更好的是,實現某種循環遍歷每個按鈕並在每次迭代時增加x值。或者,查看flowlayout管理器,他們會自動處理這個問題。