因此,我添加了我的標籤和所有內容,但仍然遇到格式和對齊問題。計算按鈕顯然應居中。我得知,使用gridbag將框架分割成座標系,當一列大於其他列時,它將調整其他列並將其丟棄(右?)。但我不明白如何解決這個問題。而且仍然無法將所有東西對準左側,因此它不會開始朝向中心。Java GUI對齊
當窗口
繼承人編譯這裏是我想要得到它的樣子,我還沒有添加一切都還沒有。
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)爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/) (簡短,獨立,正確的例子)。 2)以最小尺寸提供ASCII藝術或簡單的GUI佈局圖,並且如果可調整大小,則具有更大的寬度和高度。 –
老實說,考慮使用[MigLayout](http://www.miglayout.com/)。它比默認的Swing佈局更好更容易。 – Veluria
我敢打賭,我只能使用swing while = /。哈哈 –