使用GridBagLayout的布局问题

时间:2014-10-14 23:55:21

标签: java swing layout-manager gridbaglayout

我想制作一个看起来或多或少看起来像下图的应用程序:

enter image description here

我已经为顶级JPanel编写了代码,但我没有将结果视为例外。我的输出是这样的:

enter image description here

这是我的代码:

public class Main extends JPanel {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Main().frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        frameinit();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void frameinit() {
        frame = new JFrame();
        frame.setTitle("Tool Title");
        frame.add(this);
        frame.setPreferredSize(new java.awt.Dimension(1000, 800));
        frame.setSize(new Dimension(1000,800));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);

        this.setLayout(new GridBagLayout());
        GridBagConstraints c;
        JLabel LTitle,Lcus,Lmode,Lsc,Lcorbank,Lavd,Lrate,Lcrate,Lvaldate,Lbc,Lr;
        JTextField Tcus,Tmode,Tsc,Tcorbank,Tavd,Trate,Tcrate,Tvaldate,Tbc,Tr;
        LTitle = new JLabel("Title");
        c = new GridBagConstraints();
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=4;
        c.gridheight=1;
        add(LTitle, c);
        Lcus = new JLabel("Customer");
        c.gridx=0;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lcus, c);
        Lmode = new JLabel("Mode");
        c.gridx=0;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lmode, c);
        Lsc = new JLabel("Sell Ccy");
        c.gridx=0;
        c.gridy=3;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lsc, c);
        Lcorbank = new JLabel("Correspondant Bank");
        c.gridx=0;
        c.gridy=4;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lcorbank, c);
        Lavd = new JLabel("Amount Value Date");
        c.gridx=0;
        c.gridy=5;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lavd, c);
        Lrate = new JLabel("Rate");
        c.gridx=2;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lrate, c);
        Lcrate = new JLabel("Coverage Rate");
        c.gridx=2;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lcrate, c);
        Lavd = new JLabel("Value Date");
        c.gridx=2;
        c.gridy=3;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lavd, c);
        Lbc = new JLabel("Buy Ccy");
        c.gridx=2;
        c.gridy=4;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lbc, c);
        Lr = new JLabel("Remarks");
        c.gridx=2;
        c.gridy=5;
        c.gridwidth=1;
        c.gridheight=1;
        add(Lr, c);
        Tcus = new JTextField();
        c.gridx=1;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tcus, c);
        Tmode = new JTextField();
        c.gridx=1;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tmode, c);
        Tsc = new JTextField();
        c.gridx=1;
        c.gridy=3;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tsc, c);
        Tcorbank = new JTextField();
        c.gridx=1;
        c.gridy=4;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tcorbank, c);
        Tavd = new JTextField();
        c.gridx=1;
        c.gridy=5;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tavd, c);
        Trate = new JTextField();
        c.gridx=4;
        c.gridy=1;
        c.gridwidth=1;
        c.gridheight=1;
        add(Trate, c);
        Tcrate = new JTextField();
        c.gridx=4;
        c.gridy=2;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tcrate, c);
        Tvaldate = new JTextField();
        c.gridx=4;
        c.gridy=3;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tvaldate, c);
        Tbc = new JTextField();
        c.gridx=4;
        c.gridy=4;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tbc, c);
        Tr = new JTextField();
        c.gridx=4;
        c.gridy=5;
        c.gridwidth=1;
        c.gridheight=1;
        add(Tr, c);
    }

}

1 个答案:

答案 0 :(得分:0)

从官方教程:

  

除非您为weightx或weighty指定至少一个非零值,否则所有组件在其容器的中心聚集在一起。

所以你只需要设置:

c.weightx=1.0;
c.weighty=1.0;

此外,您可能需要设置一些值来锚定和填充。

锚点和填充的默认值在大多数情况下都能很好地工作,但它们对于进一步自定义每个组件的行为很有用。例如,如果您希望JTextField尽可能宽c.fill=GridBagConstraints.HORIZONTAL,或者您希望它触及右边距而不是左边距c.anchor=GridBagConstraints.LINE_END ......