1

在此处输入图像描述

以下是我用于将 MS Access 数据库连接到 Java 程序的代码。该按钮与IDNext的开头不对齐: JLabel

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

    }  

请告诉我出了什么问题。

4

2 回答 2

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;

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

于 2012-12-29T15:36:50.557 回答
0

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

于 2012-12-29T15:36:01.210 回答