2014-09-21 74 views
1

在此JFrame中帶有4列的GridBagLayout,棕色線應爲列1和2之間的限制,並且「確定」和「取消」按鈕應位於此限制的兩側:GridBagLayout:柵格寬度大於1時的對齊和寬度

enter image description here

的問題:

  • OK +取消設置不與其他按鍵居中。
  • 左右JTextArea不具有相同的寬度。

當我期待第1列和第2列相等時,第1列似乎具有零寬度。

使用的代碼:

import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 


public class GblSO extends JFrame { 

    // Instance variables 
    GridBagConstraints gbc = new GridBagConstraints(); 

    public GblSO() { 
     // Set frame 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLayout(new GridBagLayout()); 

     // Text areas 
     JTextArea left = new JTextArea("Left!"); 
     JTextArea right = new JTextArea("Right!"); 
     setConstraints(1, 1, GridBagConstraints.BOTH, null); 
     addToFrame(left, 0, 1, 1, 5, GridBagConstraints.CENTER); 
     addToFrame(right, 3, 1, 1, 5, GridBagConstraints.CENTER); 

     // Transfer buttons 
     JButton addBtn = new JButton(">"); 
     JButton rmvBtn = new JButton("<"); 
     setConstraints(0, 0, GridBagConstraints.NONE, new Insets(3, 5, 3, 5)); 
     addToFrame(addBtn, 1, 1, 2, 1, GridBagConstraints.CENTER); 
     addToFrame(rmvBtn, 1, 3, 2, 1, GridBagConstraints.CENTER); 

     // OK/Cancel buttons 
     JButton okBtn = new JButton("OK"); 
     JButton canBtn = new JButton("Cancel"); 
     setConstraints(0, 0, GridBagConstraints.NONE, new Insets(15, 4, 15, 4)); 
     addToFrame(okBtn, 0, 6, 2, 1, GridBagConstraints.EAST); 
     addToFrame(canBtn, 2, 6, 2, 1, GridBagConstraints.WEST); 

     // Show 
     pack(); 
     setVisible(true); 
    } 

    private void setConstraints(double weightx, double weighty, int fill, Insets insets) { 
     gbc.weightx = weightx; // how much cell resizes 
     gbc.weighty = weighty; // " 
     gbc.fill = fill; // how component fills cell 
     gbc.insets = (insets == null ? new Insets(0, 0, 0, 0) : insets); 
    } 

    private void addToFrame(Component comp, 
      int gridx, int gridy, int gridwidth, int gridheight, int anchor) { 
     gbc.gridx = gridx; 
     gbc.gridy = gridy; 
     gbc.gridwidth = gridwidth; 
     gbc.gridheight = gridheight; 
     gbc.anchor = anchor; 
     add(comp, gbc); 
    } 

    public static void main(String[] args) { 
     new GblSO(); 

    } 

} 

對於只有一個測試:如果我添加><按鈕將JFrame分別在柱1和2,並且不跨越多個列,列1和2被迫具有相同的寬度,並且底部的按鈕現在居中。

代碼改爲:

addToFrame(addBtn, 1, 1, 1, 1, GridBagConstraints.CENTER); 
addToFrame(rmvBtn, 2, 3, 1, 1, GridBagConstraints.CENTER); 

結果:

enter image description here

兩個JTextArea仍然有不同的寬度:-(,顯然><不會對齊

我該如何解決這個問題,使按鈕居中,而兩個JTextArea有相同的寬度?感謝您的幫助。

回答

0

那是因爲你沒有對每一列,這是某種需要GridBagLayout工作正如人們所預料添加任何成分(此代碼是由該tutorial啓發)。

現在開始的幾句話:

你在你掛教程注意到了,OK取消按鈕與JTextArea,不是整體上的部件對齊?

GridBagLayout一起使用時,最好爲每個要添加的組件創建一個新的GridBagConstraint。它可以防止您忘記重置屬性,這很難排除故障。

然後,如果你希望你的OK取消按鈕是中心對齊與你的上部件,最簡單的是創建兩個JPanel:一個與上部組件,另外一個與你的按鈕。您仍然可以使用GridBagLayout作爲上方面板,而默認使用(FlowLayout)作爲按鈕。你會把它們放到BorderLayout的位置CENTERSOUTH,你會很開心。需要注意的是面板是居中對齊,而不是OK/之間的空間取消按鈕和</>按鈕。

回到解決您的問題:你必須在第一(或最後)符合weightx集中添加「空」組件(ContainerJPanel,emtpy JLabel,...),以1.0所以細胞實際上是填充X方向,weighty設置爲0.0,因此它們在Y方向上不可見(反之亦然,如果要使用gridheight而不是gridwidth)。

... 
setLayout(new GridBagLayout()); 

setConstraints(1, 0, GridBagConstraints.BOTH, null); 
addToFrame(new Container(), 0, 0, 1, 1, GridBagConstraints.CENTER); 
addToFrame(new Container(), 1, 0, 1, 1, GridBagConstraints.CENTER); 
addToFrame(new Container(), 2, 0, 1, 1, GridBagConstraints.CENTER); 
addToFrame(new Container(), 3, 0, 1, 1, GridBagConstraints.CENTER); 

// Text areas 
... 

這樣細胞就會存在,你會得到預期的結果。

Correct alignment