GridBagLayout没有正确排列按钮

时间:2012-12-29 15:30:52

标签: java sql swing gridbaglayout

enter image description here

以下是用于将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();
        }

    }  

请告诉我出了什么问题。

2 个答案:

答案 0 :(得分: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;

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

答案 1 :(得分:0)

您使用gridx = 1开始第二行。位置编号为0!