简体   繁体   English

Java BoxLayout给出错误的结果

[英]Java BoxLayout gives Incorrect Results

I've got two JPanels that I want to be arranged, one on top of the other, inside of a larger JPanel. 我有两个要安排的JPanel,一个在另一个JPanel内,位于一个较大的JPanel内。 "panel_controls" overrides .getPreferredSize(). “ panel_controls”覆盖.getPreferredSize()。 Code: 码:

public final class GUIPanelMain extends JPanel {
    //...

    private JPanel panel_images;
    private JPanel panel_controls;

    //...

    private void addPanels() {
        new BoxLayout(this,BoxLayout.Y_AXIS); //Bleh!  Still is horizontal!
        this.add(panel_images);
        this.add(panel_controls);
    }

    //...
}

Unfortunately, the layout is set up horizontally anyway. 不幸的是,无论如何该布局都是水平设置的。 It's only when I resize the JPanel that the components all go in the right places (vertically, in this case). 只有当我调整JPanel的大小时,所有组件都放在正确的位置(在这种情况下为垂直放置)。

I read the following in the documentation: "BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments" 我在文档中阅读了以下内容:“ BoxLayout尝试使列中的所有组件与最宽的组件一样宽。如果失败,它将根据其X对齐水平对齐它们”

What are the conditions for failure, then? 那么,失败的条件是什么? How can I force it to be vertically aligned? 如何强制将其垂直对齐?

Thanks, 谢谢,

try 尝试

this.setLayout( new BoxLayout(this, BoxLayout.Y_AXIS));

You are basically creating a new instance of BoxLayout that goes unused and unreferenced. 您基本上是在创建一个BoxLayout的新实例,该实例将变为未使用和未引用的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM