Java GridBagLayout未正确对齐按钮

Java GridBagLayout未正确对齐按钮,java,sql,swing,gridbaglayout,Java,Sql,Swing,Gridbaglayout,下面是我用来将MS Access数据库连接到Java程序的代码。Next按钮与JLabelID的开头不对齐: public DisplayScreen(){ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e1) { // TODO Auto-gene

下面是我用来将MS Access数据库连接到Java程序的代码。
Next
按钮与
JLabel
ID的开头不对齐:

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

    }  

请告诉我出了什么问题。

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

    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;

因为您希望它从行的开头开始,并填充两列。

+1表示图片和非常干净且格式化的代码。:)@brano88啊,我应该想办法在每次发布问题时通知你。转到我的个人资料,查看我的任何问题,它们都有图像:)