【问题标题】:How to align components center in the JPanel using GridBagLayout?如何使用 GridBagLayout 在 JPanel 中对齐组件中心?
【发布时间】:2013-06-10 16:38:23
【问题描述】:

当我尝试对齐我的组件时,它要么向左要么向右。

所以我只是想要解决这个问题的解决方案,并告诉我如何将面板的大小设置为 400 x 350 像素。

这是我的代码....titleLabelResultLabel 应该居中对齐

public TimeGui() {

    layout = new GridBagLayout();
    setSize(400, 350);  //**Its not working**
    setBackground(Color.LIGHT_GRAY);
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    setBorder(new TitledBorder(new EtchedBorder(), "Time Conversion") );

    setLayout(layout);
    layoutConstraints = new GridBagConstraints();       
    textField1 = new JTextField(10);
    textField2 = new JTextField(10);

    String[] names1 = {"Seconds", "Minutes", "Hours", "Days", "Weeks"};


    comboBox1 = new JComboBox<>(names1);
    comboBox2 = new JComboBox<>(names1);

    titleLabel = new JLabel("Time Conversion Unit", JLabel.CENTER);
    resultLabel = new JLabel("Result Label");
    equalLabel = new JLabel("=");

    convertButton = new JButton("Convert");


    layoutConstraints.fill = GridBagConstraints.HORIZONTAL;
    Insets inset = new Insets(10, 10, 10, 10);
    layoutConstraints.anchor = GridBagConstraints.CENTER;

    addComponent(titleLabel, 0, 0, 2, 2, inset ); // I tried (0,1,2,2) 



    addComponent(comboBox1, 3, 0, 2, 3, inset);

    addComponent(comboBox2, 3, 2, 2, 3, inset);

    addComponent(textField1, 6, 0, 1, 2, inset);

    addComponent(equalLabel, 6, 1, 1, 2, inset);

    addComponent(textField2, 6, 2, 1, 2, inset);

    addComponent(resultLabel, 8, 1, 2, 1, inset);

    addComponent(convertButton, 10, 0, 2, 2, inset);

}

private void addComponent(Component component, int row,
        int column, int width, int height, Insets inset1) {
    layoutConstraints.gridx = column;
    layoutConstraints.gridy = row;
    layoutConstraints.gridwidth = width;
    layoutConstraints.gridheight = height;
    layoutConstraints.insets = inset1;
    layout.setConstraints(component, layoutConstraints);
    add(component);
}
}

【问题讨论】:

  • 在没有图像的情况下,如果您至少可以提供一个SSCCE,那么您至少可以在没有任何添加的情况下运行它。只需将您的图片上传到任何网站,例如 imgurdropbox4shared。任何具有更高权限的人都会将其添加到您的问题中
  • 好的,告诉我如何设置面板的大小。

标签: java swing gridbaglayout


【解决方案1】:

问题在于您的 gridwidth 和您的 fill 属性...

基本上我改变的只是......

addComponent(titleLabel, 0, 0, GridBagConstraints.REMAINDER, 2, inset); // I tried (0,1,2,2) 
addComponent(comboBox1, 3, 0, 1, 3, inset);
addComponent(comboBox2, 3, 2, 1, 3, inset);
addComponent(textField1, 6, 0, 1, 2, inset);
addComponent(equalLabel, 6, 1, 1, 2, inset);
addComponent(textField2, 6, 2, 1, 2, inset);
layoutConstraints.fill = GridBagConstraints.NONE;
addComponent(resultLabel, 8, 0, GridBagConstraints.REMAINDER, 1, inset);
addComponent(convertButton, 10, 0, GridBagConstraints.REMAINDER, 2, inset);

你可以和其他几个人一起玩。

至于定义面板的实际大小,你能做的最好的就是覆盖TimeGuigetPreferredSize方法...

@Override
public Dimension getPreferredSize() {
    return new Dimension(400, 350);
}

这将向父容器“建议”您希望布局的大小。请记住,这是一个可选值,布局管理器完全可以忽略它。

【讨论】:

  • 这取决于您使用什么布局管理器来显示它。首先覆盖我的答案底部提到的getPreferredSize
  • 你好 MadProgrammer 我仍然在努力设置组件大小。
  • 我为 Jpanel 编写了 getPreferredSize 方法,但我无法为组件编写。我该如何解决这个...
猜你喜欢
  • 2015-08-01
  • 2012-07-09
  • 2013-07-24
  • 2010-11-19
  • 2018-01-13
  • 2021-10-02
  • 2012-10-05
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多