2017-02-26 56 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