Java面板忽略setSize。

huangapple go评论78阅读模式
英文:

Java Panel ignores setSize

问题

我遇到了与[此处][1]描述的相同问题,但是没有一个答案对我起作用。

我有一个带有`BoxLayout`的`Panel`,其轴线为Y轴,因为我想要将元素堆叠在彼此下方,但我想要能够将元素向右对齐和向左对齐,所以现在我将其他面板插入到这个面板中:

```java
arrayList.forEach(message -> {
    JLabel label = new JLabel("[" + message.getSender() + " " + message.when() + "] : " + message.message(), SwingConstants.LEFT);
    Panel panel = null;
    try {
        panel = new Panel(new FlowLayout(message.getSender().equals(this.client.getName()) ? FlowLayout.LEFT : FlowLayout.RIGHT));
        panel.add(label);
        panel.setSize(panel.getSize().width, label.getPreferredSize().height);
        panel.setPreferredSize(new Dimension(panel.getSize().width, label.getPreferredSize().height));
        label.setBackground(message.getSender().equals(client.getName()) ? Color.GREEN : Color.BLUE);
        label.setForeground(message.getSender().equals(client.getName()) ? Color.BLACK : Color.WHITE);
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
    label.setOpaque(true);
    chat.add(panel, BorderLayout.PAGE_START);
});

然而,这并不起作用,因为结果如下所示:

Java面板忽略setSize。

然而,预期的结果应该是一条消息紧接着另一条消息,消息之间没有任何空隙... 我错过了什么吗?


<details>
<summary>英文:</summary>

I have the same problem described [here][1] but none of the answer worked out for me.

I have a `Panel` with a `BoxLayout` with `Axis = Y Axes`, because I would like to stack elements one below the other, but I would like to be able to align the elements to the right and to the left, so now I&#39;m inserting to this Panel, other Panels: 

    arrayList.forEach(message -&gt; {
        JLabel label = new JLabel(&quot;[&quot; + message.getSender() + &quot; &quot; + message.when() + &quot;] : &quot; + message.message(), SwingConstants.LEFT);
        Panel panel = null;
        try {
            panel = new Panel(new FlowLayout(message.getSender().equals(this.client.getName()) ? FlowLayout.LEFT : FlowLayout.RIGHT));
            panel.add(label);
            panel.setSize(panel.getSize().width, label.getPreferredSize().height);
            panel.setPreferredSize(new Dimension(panel.getSize().width, label.getPreferredSize().height));
            label.setBackground(message.getSender().equals(client.getName()) ? Color.GREEN : Color.BLUE);
            label.setForeground(message.getSender().equals(client.getName()) ? Color.BLACK : Color.WHITE);
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
        label.setOpaque(true);
        chat.add(panel, BorderLayout.PAGE_START);
    });

However this is not working since the result is the following:

[![result][2]][2]

However the supposed result should be like one message below the other without any kind of space between the messages... what am I missing?

  [1]: https://stackoverflow.com/questions/21649366/jpanel-not-working-with-setsize-setprefferedsize
  [2]: https://i.stack.imgur.com/h6Zro.png

</details>


# 答案1
**得分**: 3

```java
chat.add(panel, BorderLayout.PAGE_START);

这个语句没有意义。它暗示你正在使用BorderLayout,而你的问题中提到你正在使用BoxLayout。在使用BoxLayout时,不需要指定约束。

&gt; 我有一个采用Y轴的BoxLayout的面板,

BoxLayout尊重组件的最大尺寸。

因此,当父面板中有额外的空间时,每个子面板都会增长,直到所有额外的空间都被使用。

不要去调整size或preferred size方法。

一个解决方案是使用一个包装面板来尊重所有子面板的首选高度。

所以你的代码应该是这样的:

Box chat = Box.createVerticalBox();

JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(chat, BorderLayout.PAGE_START);

frame.add(wrapper, BorderLayout.CENTER);

现在,“chat”面板的高度将尊重添加到其中的每个组件的首选高度。
英文:
chat.add(panel, BorderLayout.PAGE_START);

That statement doesn't make sense. It implies you are using a BorderLayout, when your question stated you are using a BoxLayout. When using a BoxLayout, you don't specify a constraint.

> I have a Panel with a BoxLayout with Axis = Y Axes,

The BoxLayout respects the components maximum size.

So when there is extra space in the parent panel each child panel will grow until all the extra space is used.

Don't play with the size or preferred size methods.

One solution is to use a wrapper panel that respects the preferred height of all the child panels.

So your code should be something like:

Box chat = Box.createVerticalBox();

JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(chat, BorderLayout.PAGE_START);

frame.add(wrapper, BorderLayout.CENTER);

Now the "chat" panel height will respect the preferred height of each component added to it.

huangapple
  • 本文由 发表于 2020年10月3日 20:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64184207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定