2015-04-20 59 views
3

因此,我添加了我的標籤和所有內容,但仍然遇到格式和對齊問題。計算按鈕顯然應居中。我得知,使用gridbag將框架分割成座標系,當一列大於其他列時,它將調整其他列並將其丟棄(右?)。但我不明白如何解決這個問題。而且仍然無法將所有東西對準左側,因此它不會開始朝向中心。Java GUI對齊

當窗口

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





    } 



} 
+0

1)爲了更快地獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整可驗證示例)或[SSCCE](http://www.sscce.org/) (簡短,獨立,正確的例子)。 2)以最小尺寸提供ASCII藝術或簡單的GUI佈局圖,並且如果可調整大小,則具有更大的寬度和高度。 –

+2

老實說,考慮使用[MigLayout](http://www.miglayout.com/)。它比默認的Swing佈局更好更容易。 – Veluria

+0

我敢打賭,我只能使用swing while = /。哈哈 –

回答

0

既然你沒有指定調整大小行爲,多數佈局管理器可以用來獲得非常相似的結果。正如@MadProgrammer提到的那樣,2個面板可能是一個好方法,其中最頂端的一個我使用GridBagLayout作爲教育目的,而最下面的一個我留給你實現自己,儘管那個不會從GridBagLayout和更簡單的佈局中受益經理可以使用(如BoxLayout)。

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

enter image description here

要回答你的具體疑慮:

  • 你失蹤這一切都對齊,以左側爲你想要的行 gc.anchor = GridBagConstraints.LINE_START;

  • 由於只有2個地方需要垂直對齊,所以沒有超過2列的要點。

  • 「計算」按鈕位於列對齊的外部(它通過兩列居中)。行gc.gridwidth = GridBagConstraints.REMAINDER;意味着它應該跨越(填充)其餘列到其右側,並且行gc.anchor = GridBagConstraints.CENTER;將其與中心對齊。

我將所有的視覺效果(顏色,字體,襯墊,插圖等)留給你。