2014-09-21 69 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

相关问题