2012-12-29 46 views
1

enter image description hereGridBagLayout中不排队的按钮正确

以下是用于一个MS Access数据库连接到一个Java程序,我的代码。该Next按钮不与JLabelID开始排队:

public DisplayScreen(){ 

     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (InstantiationException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IllegalAccessException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (UnsupportedLookAndFeelException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     this.setLayout(new GridBagLayout()); 
     this.setVisible(true); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     Insets in = new Insets(2,2,2,2); 
     gbc.insets = in; 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(l1,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(f1,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(l2,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(f2,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(l3,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(f3,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(l4,gbc); 

     gbc.gridx++; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 
     this.add(f4,gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 1; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     this.add(next,gbc); 

     gbc.gridx++; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 2; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     this.add(last,gbc); 

     gbc.gridx += 2; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 2; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     this.add(first,gbc); 

     gbc.gridx += 2; 
     gbc.gridheight = 1; 
     gbc.gridwidth = 2; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     this.add(prev,gbc); 

     this.setResizable(false); 
     this.pack(); 
     try{ 
      r = new Query().getResultSet(); 
      r.next(); 
      f1.setText(r.getString("studID")); 
      f2.setText(r.getString("fName")); 
      f3.setText(r.getString("lName")); 
      f4.setText(r.getString("fee")); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

请告诉我哪里出了问题。

+1

+1为图片和非常干净和格式化的代码。 :) –

+1

@ brano88啊,我应该找到一种方式来通知你,我每次发布一个问题。去我的个人资料,看看我的任何问题,他们都有图片:) –

回答

2
gbc.gridx = 1; 
    gbc.gridy = 1; 
    gbc.gridheight = 1; 
    gbc.gridwidth = 1; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(next,gbc); 

    gbc.gridx++; 

应该

gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.gridheight = 1; 
    gbc.gridwidth = 2; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    this.add(next,gbc); 

    gbc.gridx += 2; 

,因为你希望它从该行的起点开始,并填写两个栏。

0

您从gridx = 1开始第二行。位置从0开始编号!当然