2012-03-26 23 views
0

在下面的代碼,當一個JPanel沒有添加到JFrame extention,程序工作正常,但後來,我註釋掉JPanel並使用了網格佈局,一切都正確,並很好地放置在JFrame的。 JPanel如何顯示對象會受到一些限制嗎?在代碼中,我註釋掉下面的語句:Java中的任何JPanel限制?

this.setLayout(new GridLayout(3,2)); 
    this.add(nameLabel); 
    this.add(name); 
    this.add(urlLabel); 
    this.add(url); 
    this.add(typeLabel); 
    this.add(type); 

,並錯誤地顯示程序,但如果上面的語句去掉註釋和下面的一個評論說:

//add some panes.. 
    JPanel panel = new JPanel(); 
    panel.add(nameLabel); 
    panel.add(name); 
    panel.add(urlLabel); 
    panel.add(url); 
    panel.add(typeLabel); 
    panel.add(type); 
    this.add(panel); 

,則程序工作正常以下是完整的代碼摘錄。

[CODE] 

public class FeedInfo extends JFrame { 
    private JLabel nameLabel = new JLabel("Name:", SwingConstants.RIGHT); 
    private JTextField name; 
    private JLabel urlLabel = new JLabel("URL",SwingConstants.RIGHT); 
    private JTextField url; 
    private JLabel typeLabel = new JLabel("Type",SwingConstants.RIGHT); 
    private JTextField type; 


    public FeedInfo() 
     { 
     super("Feed Information"); 
     this.setSize(400,105); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     String response1 = JOptionPane.showInputDialog(null,"Enter the site name:"); 
     name = new JTextField(response1,20); 
     String response2 = JOptionPane.showInputDialog(null,"Enter the site address:"); 
     url = new JTextField(response2, 20); 

     String[] choices ={"Personal","Commercial","Unkown"}; 
     int response3 = JOptionPane.showOptionDialog(null, "what type of site is it?", 
      "site Type",0, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]); 

     type = new JTextField(choices[response3],20); 
     //add some panes.. 
     JPanel panel = new JPanel(); 
     panel.add(nameLabel); 
     panel.add(name); 
     panel.add(urlLabel); 
     panel.add(url); 
     panel.add(typeLabel); 
     panel.add(type); 
     this.add(panel); 


     /*this.setLayout(new GridLayout(3,2)); 
     this.add(nameLabel); 
     this.add(name); 
     this.add(urlLabel); 
     this.add(url); 
     this.add(typeLabel); 
     this.add(type); 
     */ 

     this.setVisible(true); 

    } 
[/CODE] 
+0

請注意,'GridLayout'不太適合顯示名稱/值對的行。一般而言,帶標籤的列應該比具有值的列要薄。在最近的時間裏,我會爲此尋找'GroupLayout',而在之前的JRE中,我可能使用了嵌套佈局。 – 2012-03-26 15:16:15

回答

2
JPanel panel = new JPanel(); 
     panel.add(nameLabel); 
     panel.add(name); 
     panel.add(urlLabel); 
     panel.add(url); 
     panel.add(typeLabel); 
     panel.add(type); 
     this.add(panel); 

當你這樣做,你要添加的JPanel的到的JFrame,但Jframe的佈局沒有變化,這意味着它在默認情況下的borderlayout。嘗試改變最後一行:add(panel, BorderLayout.CENTER); 您可以閱讀更多有關的JFrame here

此外,一個JPanel具有的FlowLayout默認,這意味着對象將在「流動線」被添加你應該設置佈局上您的JPanel到得到想要的結果

3

JPanel本身並不進行佈局,它採用了LayoutManager。在你的情況下,你想覆蓋默認FlowLayout並使用GridLayout(就像你爲你的JFrame做的那樣)。所以呢!

JPanel panel = new JPanel(new GridLayout(3, 2)); 
... 
+1

JPanels自己執行佈局。他們默認有一個FlowLayout。 – Michael 2012-03-26 15:22:43

+0

儘管將GridLayout添加到他的JPanel會有所幫助,但仍然投票表決你的答案。 – Michael 2012-03-26 15:34:35

+0

@Michael:你的第一個陳述被你的第二個陳述駁斥。我說它不會自己做佈局,這是真的 - 它委託給LayoutManager。 FlowLayout是它使用的默認佈局管理器。對不起,我沒有試圖說「他們不默認佈局」。 – 2012-03-26 15:47:16

1

有兩個重要的事實要知道。 JFrame默認使用BorderLayout。 JPanel默認使用Flowlayout。這就是爲什麼您會看到不同的行爲,具體取決於您要將其他組件添加到哪個組件。

的FlowLayout使用一個JPanel,你幾乎總是要改變它,所以當幾乎毫無價值。 @Mark Peters向你展示瞭如何做到這一點。 BorderLayout幾乎總是用於頂級容器,我很少改變它。我傾向於將BoxLayout幾乎專門用於我的嵌套JPanel。

2

您的程序無法正常運行,與代碼的第二部分,因爲您使用

JPanel panel = new JPanel();

默認情況下,這條線,你可以從JPanel的API看到,您正在創建一個佈局爲FlowLayout的JPanel。該JPanel API報告,關於空的構造器:

JPanel() 
Creates a new JPanel with a double buffer and a flow layout. 

通常,流佈局是一個JPanel最簡單的佈局,這是不可能的負荷擺動內容的理解和簡單的方法。您使用的GridLayout提供了將組件加載到網格中的能力,這非常適合顯示Java應用程序的GUI。

現在,你知道,因爲該程序,一個基本的FlowLayout,未如預期,如在代碼的第一部分,當你使用一個網格佈局顯示您的所有Swing組件。 如果你想創建一個JPanel用GridLayout的,只需鍵入:

JPanel panel = new JPanel(new GridLayout(3, 2)); 

this頁和here大約有JPanel並佈局一些教程。