2017-02-26 55 views
-2

目前,我有这个代码,我有工作:使用的BoxLayout来调整JPanels在GUI

this.getContentPane().add(wwjPanel, BorderLayout.EAST); 
     if (includeLayerPanel) 
     { 
      this.controlPanel = new JPanel(); 
      controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS)); 
      this.layerPanel = new LayerPanel(this.getWwd()); 
      this.controlPanel.add(new FlatWorldPanel(this.getWwd()),Box.createRigidArea(new Dimension(1000, 1000))); //This is the top pan 
      this.controlPanel.add(this.getStates(), Box.createRigidArea(new Dimension(0, 100))); 
      this.controlPanel.add(this.getPanelAlerts(), Box.createRigidArea(new Dimension(0,100))); 

      this.getContentPane().add(this.controlPanel, BorderLayout.WEST);//This is the whole panel on the left 
     } 

我试图调整JPanels,叫做ControlPanel控制在这里,每一个都有自己独特的大小在我的GUI。我是用java构建GUI的新手,并且我拥有的大部分代码都是从另一个文件中提取的。我试图加入的代码是这些新面板,如我的代码中所述,并尝试调整它们的大小。 BoxLayout是我想用来获得我想要的效果吗?另外,当我使用createRigidArea它似乎工作,但如果我继续改变你传递给它的x和y值,似乎没有任何事情发生。我的意思是,我没有看到任何视觉差异通过更改值,我已经使用值范围从0-1000。

谢谢。

回答

2
this.controlPanel.add(new FlatWorldPanel(this.getWwd()),Box.createRigidArea(new Dimension(1000, 1000))); 

Box.createRigidArea(...)没有做任何事。 add(...)方法的第二个参数是布局管理器使用的约束,而BoxLayout不期望任何约束,因此应该忽略它。

如果你想在垂直堆叠的面板之间,那么你需要将其添加为一个单独的组件,你可能会使用Box.createVerticalStrut()垂直空间:

this.controlPanel.add(new FlatWorldPanel(this.getWwd())); 
this.controlPanel.add(Box.createVerticalStrut(50)); 

FlatWorldPanel的大小,是由组件,您决定添加到它。

有关更多信息和工作示例,请参阅How to Use BoxLayout上的Swing教程部分。

+0

这是有道理的,我在你链接到的文档中看到。但是,从你的观点来看,它已经清除了。 – MuffinMan1042