2012-06-07 61 views
2

我已經附加其下述Border傳說適用截圖:嵌套JPanels和GridBagLayout的不是 「包裝」 組件

黃色= JPanelBorderLayout

藍= JPanelGridBagLayout

紫紅色= JPanelFlowLayout

有兩個面板沒有被擋在顏色的warr螞蟻提及:

1)顯示單詞「主要」的標題面板;此面板在「黃色」面板中的BorderLayout.NORTH

2)設備圖像所在的圖像面板;這個面板是同級「倒掛金鐘」

Nested Layout Problem

「藍色」是在BorderLayout.CENTER在「黃」,而「倒掛金鐘」和圖像面板給出下列約束條件:

GridBagConstraints c = new GridBagConstraints(); 

c.weightx = 1.0; 
c.weighty = 1.0; 
c.gridx = 0; 
c.gridy = 0; 
c.anchor = GridBagConstraints.NORTHWEST; 
c.insets = new Insets(0, 10, 0, 0); 
c.fill = GridBagConstraints.BOTH; 

//"Blue".add(imagePanel, c); 

c.weighty = 0.80;  
c.gridy = 1; 
c.fill = GridBagConstraints.HORIZONTAL; 


//"Blue".add("Fuchsia", c); 

作爲你可以從圖像中發現,我試圖擺脫「紫紅色」下方「藍色」中的「浪費」空間。我似乎不能用GridBagConstraints這樣做,所以我只是使用了錯誤的LayoutManager?在我看來,像「藍色」,誰在CENTERBorderLayout只是給每個孩子JPanel一半的可用空間和保留剩餘空間,而不是向上收縮。我在這裏錯過了什麼?這是否只是在「Fuchsia」上設置首選或最大尺寸的問題?它似乎不會讓我想要成爲我想去的地方,因爲圍繞「Fuchsia」(我的顏色編碼)覆蓋的邊界就是我希望組件結束的地方。

+0

假如你試圖使用'C .fill = GridBagConstraints.BOTH/c.fill = GridBagConstraints.VERTICAL;',而不是o f'c.fill = GridBagConstraints.HORIZONTAL;'?或者你是否明確地爲代碼中的任何地方的'JFrame/JPanel'定義了任何大小? –

+0

@nIcEcOw代碼中沒有明確的大小定義。我會盡快嘗試填寫建議。 –

+0

我認爲這是你的黃色'JFrame'的父級佈局管理器。哪一個?我的[測試代碼](http://pastebin.com/PeM98u1t)顯示了所需的行爲。你介意發佈[SSCCE](http://pscode.org/sscce.html)嗎? – Howard

回答

4

LAYOUT SNAPSHOT

看一看這個輸出,從這個代碼示例:

import java.awt.*; 
import javax.swing.*; 

public class LayoutTest 
{ 
    private void displayGUI() 
    { 
     JFrame frame = new JFrame("Layout Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setOpaque(true); 
     contentPane.setBackground(Color.YELLOW); 
     contentPane.setLayout(new BorderLayout(2, 2)); 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     JLabel headingLabel = new JLabel("Primary"); 
     topPanel.add(headingLabel); 
     contentPane.add(topPanel, BorderLayout.PAGE_START); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.setOpaque(true); 
     centerPanel.setBackground(Color.BLUE); 
     centerPanel.setLayout(new GridBagLayout()); 

     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weightx = 1.0; 
     gbc.weighty = 0.2; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 

     JPanel imagePanel = new JPanel(); 
     JLabel imageLabel = null; 
     try 
     { 
      imageLabel = new JLabel(
          new ImageIcon(
           new java.net.URL(
            "http://pscode.org/" 
            + "tame/screenshot/" 
            + "landscape/slider1.gif"))); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     imagePanel.add(imageLabel); 
     centerPanel.add(imagePanel, gbc); 

     JPanel detailsPanel = new JPanel(); 
     detailsPanel.setOpaque(true); 
     detailsPanel.setBackground(Color.WHITE); 
     detailsPanel.setBorder(
         BorderFactory.createEmptyBorder(
               5, 5, 5, 5)); 
     detailsPanel.setLayout(new GridLayout(0, 1, 5, 5)); 

     JLabel statusLabel = new JLabel("Chassis Status : "); 
     JLabel usageLabel = new JLabel("Bandwidth Usage : "); 
     JLabel fanLabel = new JLabel("Fan Status : "); 

     detailsPanel.add(statusLabel); 
     detailsPanel.add(usageLabel); 
     detailsPanel.add(fanLabel); 

     gbc.fill = GridBagConstraints.BOTH; 
     gbc.weighty = 0.8; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     gbc.gridheight = 3; 
     centerPanel.add(detailsPanel, gbc); 

     contentPane.add(centerPanel, BorderLayout.CENTER); 

     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new LayoutTest().displayGUI(); 
      } 
     }); 
    } 
} 
+0

這看起來非常有前途。我只是在這裏運行代碼,並不確定它是否適合賬單,因爲具有三個JLabel的面板看起來大小相似,但是在移除其中兩個之後,我可以清楚地看到該尺寸工作正常。稍後會更新。 –

+0

Ahha,在我看來,你的代碼中唯一的問題就像我在評論中提到的那樣,而不是使用HORIZONTAL,你只能使用BOTH,這樣在任何方向上都不會留出空間。雖然你可以從我的代碼中註釋出'gbc.gridheight = 3;'這行代碼,但它的工作原理與你的預期非常相似:-) –

+0

+1好,但我默認試圖避免在論壇上使用GBC: - ) – mKorbel

4

不使用的GridBagLayout可能是

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.border.LineBorder; 

public class NestedLayout { 

    private JFrame frame = new JFrame(); 
    private JPanel yellowNorthPanel = new JPanel(); 
    private JPanel yellowPanel = new JPanel(); 
    private JPanel bluePanel = new JPanel(); 
    private JPanel fuchsiaTopPanel = new JPanel(); 
    private JPanel fuchsiaBottonPanel = new JPanel(); 

    public NestedLayout() { 
     yellowNorthPanel.setBorder(new LineBorder(Color.yellow, 5)); 
     yellowPanel.setLayout(new BorderLayout()); 
     yellowPanel.setBorder(new LineBorder(Color.yellow, 5)); 
     bluePanel.setLayout(new BorderLayout(5, 5)); 
     bluePanel.setBorder(new LineBorder(Color.blue, 5)); 
     fuchsiaTopPanel.setBorder(new LineBorder(Color.cyan, 5)); 
     fuchsiaBottonPanel.setBorder(new LineBorder(Color.cyan, 5)); 
     bluePanel.add(fuchsiaTopPanel, BorderLayout.NORTH); 
     bluePanel.add(fuchsiaBottonPanel); 
     yellowPanel.add(bluePanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(yellowNorthPanel, BorderLayout.NORTH); 
     frame.add(yellowPanel); 
     //frame.pack(); 
     frame.setSize(400, 300); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new NestedLayout(); 
      } 
     }); 
    } 
} 
+0

我想這就是現在發生的事情。我真的希望Fuchsia'JPanel'比圖像面板更小(現在),並且隨着需要顯示更多信息,可能會將其擴大爲更大。 –

+0

有一個你的小錯誤,'fuchsiaTopPanel'可以通過調用(取消註釋'pack()')或設置(最後一個屬性)'PreferredSize'來接受來自'JComponent'的大小,我認爲沒有問題在這種情況下在屏幕上適當的尺寸 – mKorbel