本文介绍了如何使用GridBagLayout在JPanel中对齐组件中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对齐我的组件时,它会左侧或右侧。

When I try to align my component it goes either left side or right side.

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

So I just want the solution to get rid of this problem, and also tell me how set the size of the panel as 400 x 350 pixel.

这是我的代码.... titleLabel ResultLabel 应该在中心对齐

Here is my code....titleLabel and ResultLabel should be aligned in center

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


推荐答案

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

The problem is with your gridwidth and your fill properties...

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

Basically all I changed was...

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

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

You could play around with a few of the others.

至于定义面板的实际大小,你可以做的最好是覆盖 TimeGui的 getPreferredSize 方法 ...

As for defining the actual size of the panel, the best you can do is to override the getPreferredSize method of the TimeGui...

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

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

Which will "suggest" to the parent container what size you would like to be laid out to. Just remember, this is an optional value and layout managers are well within there rights to ignore it.

这篇关于如何使用GridBagLayout在JPanel中对齐组件中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:21
查看更多