2015-11-03 74 views
2

我想利用現有的佈局,如GridBagLayout實現以下電網系統爲我的Java應用程序:搖擺 - 在一個JFrame多個佈局結構

Layout

我設法創建一個類似的佈局,但沒有較小的按鈕只使用GridBagLayout,但當我嘗試添加這些較小的按鈕時,它不工作,因爲它不是相同的單元大小。

我認爲解決方案可以通過向JFrame添加多個佈局,每個佈局代表全局佈局的一部分,但我找不到要使用的正確佈局。

是否有一個GridBagLayout可用於創建所有較大的方形部分,還有兩個大的部分,一個包含左上角的項目,第二個包含以下8個較小的按鈕?

謝謝。

+0

這一切都取決於你想要如何調整單元格大小。較小的4x2網格可以在它自己的容器中,這將簡化問題 – MadProgrammer

+0

實際上整個JFrame不能調整大小,所以我可以將4x2網格放在另一個容器中而沒有問題。 – AntoineB

回答

4

我在說的是「使用複合佈局」,其中您使用一個或多個子容器來爲特定區域執行特定的佈局,並將這些容器與另一個佈局組合到另一個容器中,像...

Layout

所以小4x2電網實際上是另一個JPanel這需要該區域的細節的照顧(和你很可能使用GridLayout),然後這個被併入大布局

import java.awt.Color; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setBackground(Color.BLACK); 
      setLayout(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.fill = GridBagConstraints.BOTH; 
      gbc.weightx = 0.75; 
      gbc.weighty = 0.16; 
      gbc.gridwidth = 3; 
      gbc.insets = new Insets(4, 4, 4, 4); 
      add(makePanel(Color.DARK_GRAY), gbc); 

      gbc.weightx = 0.25; 
      gbc.gridx = 3; 
      gbc.gridwidth = 1; 
      add(makePanel(Color.ORANGE), gbc); 

      gbc.gridy++; 
      add(makePanel(Color.DARK_GRAY), gbc); 

      gbc.gridx = 0; 
      gbc.gridwidth = 3; 
      gbc.weightx = 0.75; 
      add(makeSmallerPane(), gbc); 

      gbc = new GridBagConstraints(); 
      gbc.weightx = 0.25; 
      gbc.insets = new Insets(4, 4, 4, 4); 
      gbc.weighty = 0.16; 
      gbc.fill = GridBagConstraints.BOTH; 
      for (int y = 2; y < 7; y++) { 
       gbc.gridx = y; 
       for (int x = 0; x < 4; x++) { 
        gbc.gridx = x; 
        Color color = Color.GRAY; 
        if (x == 3) { 
         color = Color.DARK_GRAY; 
        } 
        add(makePanel(color), gbc); 
       } 
      } 
     } 

     protected Component makePanel(Color color) { 
      JPanel panel = new JPanel(); 
      panel.setBackground(color); 
      return panel; 
     } 

     protected Component makeSmallerPane() { 
      JPanel panel = new JPanel(new GridBagLayout()); 
      panel.setOpaque(false); 
      panel.setLayout(new GridBagLayout());; 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.fill = GridBagConstraints.BOTH; 
      gbc.weightx = 0.25; 
      gbc.weighty = 0.5; 
      gbc.insets = new Insets(0, 0, 0, 4); 

      int topGap = 0; 
      int bottomGap = 4; 
      for (int y = 0; y < 2; y++) { 
       gbc.gridy = y; 
       for (int x = 0; x < 4; x++) { 
        gbc.gridx = x; 
        switch (x) { 
         case 0: 
          gbc.insets = new Insets(topGap, 0, bottomGap, 4); 
          break; 
         case 3: 
          gbc.insets = new Insets(topGap, 4, bottomGap, 0); 
          break; 
         default: 
          gbc.insets = new Insets(topGap, 4, bottomGap, 4); 
          break; 
        } 
        panel.add(makePanel(Color.DARK_GRAY), gbc); 
       } 
       topGap = 4; 
       bottomGap = 0; 
      } 

      return panel; 
     } 

    } 

} 
+0

非常感謝你,這幾乎是我正在尋找的:)我遇到的唯一問題,並且在我的測試後我無法修復的事實是,面板下方的橙色面板在4x2右側, 4x2面板的高度。如果可能的話,我希望4x2面板佔據這個1x1面板的高度,這個高度應該是窗口右側的其他面板的高度。我設法找到了合適的尺寸,但4x2面板保持不變,不適合。 – AntoineB

+0

在我的例子中,我對所有約16%的行給予了相同的高度(我認爲它實際上應該是14%)。這不能保證,因爲諸如組件的最小尺寸之類的其他事實可能在確定提供給該行的總高度方面起作用。此外,類似'fill'屬性的東西也可能會產生影響 – MadProgrammer

+0

是的,我注意到了16%的高度(並將它改爲14:P),但即使在您自己的屏幕截圖中,您也可以看到此面板有更大的其他人的身高低於它。在我的情況下,我在面板內添加了按鈕,差異更大。他們是一種避免這種情況的方法嗎?它看起來像組件有一個默認的最小填充或類似的東西:/ – AntoineB

0

不是直接將按鈕添加到JFrame中,而是將它們放入具有自己GridBagLayout的JPanel中,該GridBagLayout具有所需的單元大小。這樣,您可以添加JPanel並將所需大小的按鈕放在裏面。