我试图将 JsplitPane
添加到我的项目中。要求是我需要在 JSplitPane
的右侧面板中添加两个 Jpanel
。
所以我所做的是首先添加 Jpanel
说它 panel1
到右侧面板并设置 BoxLayout.Y-AXIS
然后添加两个panel1
内的面板。
现在,两个面板中的第一个面板有 BoxLayout
,我希望该面板的宽度为 panel1
大小,但我无法做到这一点。
有人知道该怎么做吗?
最佳答案
I was trying to add
JsplitPane
into my project.Requirement is i need to add twoJpanel
inside right panel ofJSplitPane
.
当使用JSplitPane
时,我们应该记住,它仅将 Pane 分为两个部分,即左和右或顶部和底部。因此,当我们再次想要在 JSplitPane
的一侧添加多个组件时,例如在我们的例子中 Right,最好使用嵌套拆分 Pane 。这意味着在分割 Pane 内创建分割 Pane 。
so what i had done is first add
Jpanel
say itpanel1
to right panel and setBoxLayout.Y-AXIS
and than add two panel insidepanel1
.now in that two panel first panel have
BoxLayout
and i want the width of this panel to be of size ofpanel1
but i am not able to do it.
如果您使用嵌套分割 Pane ,您可能不必创建一个额外的父级JPanel
(如您所说的panel1
)。实际上,分割 Pane 用于将 Pane 分为两部分。因此,通过使用嵌套拆分 Pane ,您将创建另一个拆分 Pane ,而不是您创建的panel1
,然后将两个子面板放入 new 创建的两个 Pane 中JSplitPane 是嵌套的。因此,您也不必考虑尺寸问题。我希望我能让你清楚并解决你的问题。
实现这一目标的简单方法是使用:
声明:
private JSplitPane jSplitPane1;
private JSplitPane jSplitPane2;
private JPanel jPanel1;
private JPanel jPanel2;
在构造函数中:
jSplitPane1 = new JSplitPane();
jSplitPane2 = new JSplitPane();
jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
jSplitPane1.setRightComponent(jSplitPane2);
jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
jSplitPane1.setTopComponent(jPanel1);
jSplitPane1.setBottomComponent(jPanel2);
上述方法是实现您想要的最简单的方法。但是,如果不嵌套拆分 Pane ,则可以使用可能不太方便的多拆分 Pane 。不过您可以看看 Oracle 的这篇旧文章:
关于java - JSplitPanel 内 Jpanel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298055/