怎样才能不让一个JLabel改变其大小时,其文本的变化?(How do you stop a JLa

2019-07-29 17:26发布

我在代码生成一些JComponents和使用网格包布局来安排他们。 我的布局由12行和3列,其中每一行由JSlider的,一个JCheckBox的和JLabel的。 下面是我用来生成UI的代码:

final int NUM_MOTORS = 12;

// This is the panel I'm adding the components to.
pnlMotorSliders.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

for (int i = 0; i < NUM_MOTORS; ++i) {
    c.gridy = i;

    // Create the slider
    JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 10, 4085, 10);
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.weightx = 0.9;
    pnlMotorSliders.add(slider, c);

    // Create the checkbox
    JCheckBox checkBox = new JCheckBox();
    checkBox.setOpaque(true);
    checkBox.setBackground(Color.blue);
    c.fill = GridBagConstraints.NONE;
    c.gridx = 1;
    c.weightx = 0.1;
    pnlMotorSliders.add(checkBox, c);

    // Create the current label
    JLabel label = new JLabel("0");
    label.setBorder(BorderFactory.createLineBorder(Color.red));
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.weightx = 0.2;
    pnlMotorSliders.add(label, c);
}

我遇到的问题是,当我设置在任何的JLabel的文本,他们改变宽度和影响布局的休息,即使我设置显示的文本的宽度比小得多的JLabel宽度。 下面的两个屏幕截图展示了我的意思(红色和蓝色的边框是用于调试目的):

我已经设置在底部的JLabel为“-12”的文字。 即使JLabel的似乎是比文字更广泛,它已经改变了它的大小,影响布局的其余部分。

为什么会出现这种情况,我能做些什么来预防呢?

Answer 1:

您可以通过设置最低固定标签的大小,和者优先最大尺寸:

label.setMinimumSize(width, height);
label.setPreferedSize(width, height);
label.setMaximumSize(width, height);

另外,还要确保设定的GridBagConstraints#填到无,但我不知道这仍然是neccessary(我认为这是)。

编辑:顺便说一句,摆脱围绕焦点的组件那些讨厌的虚线,你可以将其设置为不可作为焦点:

slider.setFocusable(false);


Answer 2:

The set-the-preferred-size solution works only if you don't have the components horizontally fill their bag in the GridBagLayout.

Another solution is to remove the weight you have placed on components in that column of your GridBagLayout. You can then control the column width manually. An easy way to do so (at design time) is to place a JLabel in the column with zero height and the specific width you desire.

Why is this? You need to dig into how GridBagLayout works:

The GridBagLayout sizes its columns based on the space required, and then uses the weights to allocate the "left over" space so that the resulting column widths then add up to the total width of the panel. The space required for each column is computed by finding the widest of the components in that column.

The space required for each component is determined by asking it what width it would prefer. In your case, a JLabel with "0" is smaller than a JLabel with "-12" and so the column is changing size.

The left over space is allocated based on the horizontal weights assigned to components in each column. The weights are totaled and percentages for each column are determined based on that column's percent of the total.

The column size is determined based on the space required PLUS the left over space. So, if all your components in the column have no weight then you'll not get any left over space, and hence not get dynamic changes.

A third solution is to just not use GridBagLayout.



Answer 3:

明确地设定使用标签的最佳尺寸JLabel#setPreferredSize(Dimension)

这样一来,你锁定组件的尺寸,并且告诉布局管理器没有调整其大小。



Answer 4:

label.setPreferredSize(new Dimension(width, height));

这是我能找到,使其工作方式。 设置一个Minimum和/或Maximum并没有为我做任何事。 此外,对我来说java的8,我需要用一个Dimension ,或有以前的,不恰当的尺寸没有变化。



文章来源: How do you stop a JLabel changing its size when its text changes?