Java GUI对齐

时间:2015-04-20 23:03:16

标签: java swing layout-manager gridbaglayout swingutilities

所以我添加了标签和所有内容,但是我仍然无法格式化和对齐所有内容。计算按钮显然应该居中。我得到了使用gridbag将框架拆分为坐标系统,当一列大于其他列时,它将调整其他列并将其关闭(对吗?)。但我无法理解如何解决这个问题。并且仍然无法将所有内容对齐到左侧,因此它不会从中心开始。

编译时会看到窗口

Have

这就是我想要的结果,我还没有添加所有内容。

Want

 JPanel p = new JPanel(new GridBagLayout());


GridBagConstraints gc = new GridBagConstraints();

//0 Column
    gc.gridx = 0;
    gc.gridy = 0;
    gc.anchor = GridBagConstraints.EAST;
    p.add(new JLabel("Mortgage amount:"),gc);


    gc.gridx = 0;
    gc.gridy =1;
    p.add(new JLabel("Mortgage term:"),gc);



    gc.gridx = 0;
    gc.gridy = 2;
    p.add(new JLabel("Interest rate:"),gc);

    gc.gridx = 0;
    gc.gridy = 3;
    p.add(new JLabel("Mortgage start date:"),gc);

    gc.gridx = 0;
    gc.gridy= 4;
    p.add(new JLabel("Monthly Payments:"),gc);

    gc.gridx = 0;
    gc.gridy = 7;
    p.add(new JLabel("Extra payments"),gc);

    gc.gridx = 0;
    gc.gridy = 8;
    p.add(new JLabel("Adding: "),gc);

    gc.gridx = 0;
    gc.gridy = 9;
    p.add(new JLabel("Adding: "),gc);

    gc.gridx = 0;
    gc.gridy = 10;
    p.add(new JLabel("Adding: "),gc);

    gc.gridx = 0;
    gc.gridy = 11;
    p.add(new JLabel("Changes paid off date to:"),gc);


    //1 Column
    gc.gridx = 1;
    gc.gridy = 0;
    p.add(new JLabel("$"),gc);

    gc.gridx = 1;
    gc.gridy = 4;
    p.add(new JLabel("$"),gc);

    gc.gridx = 1;
    gc.gridy = 8;
    p.add(new JLabel("$"),gc);

    gc.gridx = 1;
    gc.gridy = 9;
    p.add(new JLabel("$"),gc);

    gc.gridx = 1;
    gc.gridy = 10;
    p.add(new JLabel("$"),gc);

    //2 column
    gc.gridx = 2;
    gc.gridy = 0;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy =1;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy = 2;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy = 3;
    p.add(new JComboBox(month),gc);

    gc.gridx= 2;
    gc.gridy = 4;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy = 8;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy = 9;
    p.add(new JTextField(8),gc);

    gc.gridx = 2;
    gc.gridy = 10;
    p.add(new JTextField(8),gc);

    //3 column
    gc.gridx = 3;
    gc.gridy = 1;
    p.add(new JLabel(" years or "),gc);

    gc.gridx = 3;
    gc.gridy = 2;
    p.add(new JLabel(" % per year"),gc);

    gc.gridx = 3;
    gc.gridy = 3;
    p.add(new JComboBox(days),gc);

    gc.gridx = 3;
    gc.gridy = 8;
    p.add(new JLabel("to your monthly mortgage payment"),gc);

    gc.gridx = 3;
    gc.gridy = 9;
    p.add(new JLabel("as an extra yearly mortgage payment every "),gc);

    gc.gridx = 3;
    gc.gridy = 10;
    p.add(new JLabel("as a one-time payment in "),gc);


    //4 column
    gc.gridx = 4;
    gc.gridy = 1;
    p.add(new JTextField(8),gc);

    gc.gridx = 4;
    gc.gridy=3;
    p.add(new JComboBox(years),gc);



    //5 column
    gc.gridx = 5;
    gc.gridy = 1;
    p.add(new JLabel(" months"),gc);

    gc.gridy=5;
    gc.anchor = GridBagConstraints.CENTER;
    p.add(new JButton("Calculate"),gc);


    add(p, BorderLayout.NORTH);





    }



}

1 个答案:

答案 0 :(得分:0)

由于您没有指定调整大小行为,因此可以使用许多布局管理器来获得非常相似的结果。正如@MadProgrammer所提到的,2个小组可能是一个很好的方法,其中最好的方法是我用GridBagLayout用于教育目的,而最下面的一个我留空以供你实现自己,尽管那个人不会从中受益{可以使用{1}}和更简单的布局管理器(例如GridBagLayout)。

BoxLayout

enter image description here

回答您的具体疑虑:

  • 你错过了这条线 public class Mort extends JFrame { public static void main(String[] args) { new Mort(); } Mort() { JPanel topPanel = new JPanel(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); JPanel tempPanel; gc.anchor = GridBagConstraints.LINE_START; // Top // 0 Column gc.gridx = 0; gc.gridy = 0; topPanel.add(new JLabel("Mortgage amount:"), gc); gc.gridy = 1; topPanel.add(new JLabel("Mortgage term:"), gc); gc.gridy = 2; topPanel.add(new JLabel("Interest rate:"), gc); gc.gridy = 3; topPanel.add(new JLabel("Mortgage start date:"), gc); gc.gridy = 4; topPanel.add(new JLabel("Monthly Payments:"), gc); gc.gridy = 5; gc.gridwidth = GridBagConstraints.REMAINDER; gc.anchor = GridBagConstraints.CENTER; topPanel.add(new JButton("Calculate"), gc); gc.gridwidth = 1; gc.anchor = GridBagConstraints.LINE_START; // 1 Column gc.gridx = 1; gc.gridy = 0; tempPanel = new JPanel(); tempPanel.add(new JLabel("$")); tempPanel.add(new JTextField(8)); topPanel.add(tempPanel, gc); gc.gridy = 1; tempPanel = new JPanel(); tempPanel.add(new JTextField(8)); tempPanel.add(new JLabel("years of")); tempPanel.add(new JTextField(8)); tempPanel.add(new JLabel("months")); topPanel.add(tempPanel, gc); gc.gridy = 2; tempPanel = new JPanel(); tempPanel.add(new JTextField(8)); tempPanel.add(new JLabel("% per year")); topPanel.add(tempPanel, gc); gc.gridy = 3; tempPanel = new JPanel(); tempPanel.add(new JComboBox()); tempPanel.add(new JComboBox()); tempPanel.add(new JComboBox()); topPanel.add(tempPanel, gc); gc.gridy = 4; tempPanel = new JPanel(); tempPanel.add(new JLabel("$")); tempPanel.add(new JTextField(8)); topPanel.add(tempPanel, gc); // Bottom JPanel bottomPanel = new JPanel(); bottomPanel.add(new JLabel("BottomPanel")); // Main JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.add(new JLabel("Mortgage Calculator")); mainPanel.add(new JSeparator()); mainPanel.add(topPanel); mainPanel.add(new JSeparator()); mainPanel.add(bottomPanel); getContentPane().add(mainPanel); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } } 它可以根据需要将所有内容对齐。

  • 超过2列没有意义,因为只有2个地方需要垂直对齐。

  • "计算"按钮位于列对齐之外(它通过两列居中)。行gc.anchor = GridBagConstraints.LINE_START;表示它应该跨越(填充)其右侧的列,行gc.gridwidth = GridBagConstraints.REMAINDER;将其与中心对齐。

我将所有视觉效果(颜色,字体,填充,插图等)留给您。