2016-10-03 24 views
2

即使指定了.weightx,.weighty和.anchor,GridBagConstraints也无法正常工作。 我需要将组件设置在左上方,但它始终设置在屏幕的中心。以下是我用于定位组件的方法。GridBagConstraints不正确对齐JRadioButton框

private void initGUI(){ 
    getContentPane().setLayout(new GridBagLayout()); 

    JPanel panel = new JPanel(); 
    panel.setPreferredSize(new Dimension(450, 450)); 

    Box buttonBox = Box.createVerticalBox(); 
    ButtonGroup buttons = new ButtonGroup(); 

    buttons.add(okOnly); 
    buttons.add(okCancel); 
    buttons.add(yesNoCancel); 
    buttons.add(yesNo); 

    buttonBox.add(okOnly); 
    buttonBox.add(okCancel); 
    buttonBox.add(yesNoCancel); 
    buttonBox.add(yesNo); 
    buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons")); 

    GridBagConstraints gridConstraints = new GridBagConstraints(); 
    gridConstraints.gridx = 0; 
    gridConstraints.gridy = 0; 
    gridConstraints.anchor = GridBagConstraints.NORTHWEST; 
    gridConstraints.weightx = 1; 
    gridConstraints.weighty = 1; 

    panel.add(buttonBox, gridConstraints); 
    getContentPane().add(panel); 
} 
+1

去过,因为我用网格包的东西,所以这可能一会儿是一个愚蠢的评论,但是:您的GridBagLayout位于内容窗格上,但是当您将buttonBox添加到其他面板时,您会传递约束条件。你确定这一切是正确的吗? – John3136

+0

@ John3136:不,它不行。见下面的代码。 –

+0

是的,问题是没有面板的布局管理器,我也添加了它。谢谢你的收获。 – programmer

回答

4

你给的GridBagLayout到contentPane,但添加一个组件,将其无的GridBagConstraints,然后给没有布局到面板的JPanel,以便它使用默认的FlowLayout,并添加组件它使用GridBagConstraints,在这种情况下没有意义的约束。

解决方法:显然不要这样做。将组件添加到使用GridBagLayout的容器时,仅使用GridBagConstraints。

其实,我只是摆脱面板JPanel的,如果你需要的是JradioButton将在左上角的集合:

import java.awt.*; 
import javax.swing.*; 

public class Gui extends JFrame { 
    private JRadioButton okOnly = new JRadioButton("Ok "); 
    private JRadioButton okCancel = new JRadioButton("Ok Cancel"); 
    private JRadioButton yesNoCancel = new JRadioButton("Yes No Cancel"); 
    private JRadioButton yesNo = new JRadioButton("Yes No"); 

    private void initGUI() { 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     getContentPane().setLayout(new GridBagLayout()); 

     // JPanel panel = new JPanel(); 
     // panel.setPreferredSize(new Dimension(450, 450)); 

     setPreferredSize(new Dimension(450, 450)); 

     Box buttonBox = Box.createVerticalBox(); 
     ButtonGroup buttons = new ButtonGroup(); 

     buttons.add(okOnly); 
     buttons.add(okCancel); 
     buttons.add(yesNoCancel); 
     buttons.add(yesNo); 

     buttonBox.add(okOnly); 
     buttonBox.add(okCancel); 
     buttonBox.add(yesNoCancel); 
     buttonBox.add(yesNo); 
     buttonBox.setBorder(BorderFactory.createTitledBorder("Buttons")); 

     GridBagConstraints gridConstraints = new GridBagConstraints(); 
     gridConstraints.gridx = 0; 
     gridConstraints.gridy = 0; 
     gridConstraints.anchor = GridBagConstraints.NORTHWEST; 
     gridConstraints.weightx = 1; 
     gridConstraints.weighty = 1; 

     // panel.add(buttonBox, gridConstraints); 
     add(buttonBox, gridConstraints); 
     // getContentPane().add(panel); 

     pack(); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    private static void createAndShowGui() { 
     new Gui().initGUI(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
}