2013-11-25 143 views
0

我對Java Swing比較陌生,並且我對理解網格佈局如何做某些事情有點麻煩,如果他們做不到,那麼網格佈局應該如何更強大去做。從網格佈局到GridBag佈局

這裏是一個程序我試圖與網格佈局

import javax.swing.*; 
import java.awt.*; 
//import java.awt.event.*; 
public class Swing24 
{ 
public static void main(String[] args) 
{ 
JFrame f1= new JFrame("Grid Layout Test"); 

f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f1.setResizable(true); 
f1.setLocation(500,200); 
f1.setSize(600,600); 

JPanel p1 = new JPanel(); 
p1.setBackground(Color.black); 
f1.add(p1); 


JButton b1= new JButton("Button 1"); 
b1.setBackground(Color.white); 

JButton b2= new JButton("Button 2"); 
b2.setBackground(Color.white); 

JButton b3= new JButton("Button 3"); 
b3.setBackground(Color.white); 

JLabel lb1=new JLabel(" Label 1"); 
lb1.setForeground(Color.orange); 
//lb1.setOpaque(true); 
lb1.setBackground(Color.yellow); 

JLabel lb2=new JLabel(" Label 2"); 
lb2.setBackground(Color.orange); 
lb2.setOpaque(true); 


GridLayout glm1=new GridLayout(2,3,0,0); 
p1.setLayout(glm1); 

p1.add(b1); 
p1.add(b2); 
p1.add(b3); 
p1.add(lb1); 
p1.add(lb2); 


f1.setVisible(true); 

} 
} 

上述程序允許我分開容器分成2行和3列。基本上我可以用一個網格佈局將容器分成m行和n列。但它會連續添加組件(butons和labels)。

問題1:如何直接將一個按鈕添加到大小爲(10,10)的網格中的單元格(4,3)? 問題2:按鈕可以在網格佈局中佔用多個單元嗎?

如果上述任何一個答案都不可行,那麼gridbag佈局如何幫助解決問題。 我嘗試使用一個按鈕的網格佈局佈局。但它被放置在中心!我怎麼能說,把它放到一個容器中的單元格(4,3)上,這個容器可以分爲大小(10,10)< 10行和10列>

回答

1

1)您不能將組件添加到特定的單元格,但在that question中,您可以找到某種類型的技巧。

2)這裏是另一個trick嵌套在單元格內,用於合併。

你可以在GridBagLayout的幫助下做所有你想要的。觀看GridBagConstraints它可以幫助您正確地佈置組件。

見的GridBagConstraints屬性:

gridwidthgridheightgridxgridyanchor

但是,如果您只想將一個組件添加到容器中,則需要在單元格(4,3)周圍留空的空間。

另請閱讀教程GridBagLayout

編輯:你可以嘗試這樣的事情

public class Form extends JFrame { 

    public Form() { 
     getContentPane().setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = 0; 
     c.weightx = 1; 
     c.weighty = 1; 
     for(int i =0;i<10;i++){ 
      c.gridx = i; 
      for(int j =0;j<10;j++){ 
       c.gridy = j; 
       if(i == 3 && j == 2){ 
        c.fill = GridBagConstraints.NONE; 
        getContentPane().add(new JButton("btn"),c); 
       } else { 
        c.fill = GridBagConstraints.BOTH; 
        JPanel p = new JPanel(); 
        p.setBorder(BorderFactory.createLineBorder(Color.red)); 
        getContentPane().add(p,c); 
       } 
      } 
     } 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     pack(); 
     setLocationRelativeTo(null); 
    } 


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

      public void run() { 
       new Form().setVisible(true); 
      } 
     }); 
    } 
} 

enter image description here

EDIT2:這不是真的細胞(4,3),但在相同的比例

public Form() { 
    getContentPane().setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.weightx = 0.2; 
    c.weighty = 0.3; 
    c.fill = GridBagConstraints.BOTH; 
    getContentPane().add(new JLabel(" "),c); 

    c.gridx++; 
    c.gridy++; 
    c.fill = GridBagConstraints.NONE; 
    getContentPane().add(new JButton("btn"),c); 

    c.weightx = 0.7; 
    c.weighty = 0.6; 
    c.gridx++; 
    c.gridy++; 
    c.fill = GridBagConstraints.BOTH; 
    getContentPane().add(new JLabel(" "),c); 

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pack(); 
    setLocationRelativeTo(null); 
} 

或真實小區( 4,3),但多於3個組件且小於100:

public Form() { 
    getContentPane().setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.weightx = 1; 
    c.weighty = 1; 
    for(int i =0;i<2;i++){ 
     getContentPane().add(new JLabel(" "),c); 
     c.gridx++; 
    } 

    for(int i =0;i<3;i++){ 
     getContentPane().add(new JLabel(" "),c); 
     c.gridy++; 
    } 

    c.gridx = 3; 
    c.gridy = 4; 
    getContentPane().add(new JButton("btn"),c); 

    for(int i =0;i<7;i++){ 
     getContentPane().add(new JLabel(" "),c); 
     c.gridx++; 
    } 

    for(int i =0;i<6;i++){ 
     getContentPane().add(new JLabel(" "),c); 
     c.gridy++; 
    } 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    pack(); 
    setLocationRelativeTo(null); 
} 
+0

非常感謝您的回答。稍後我會回到網格佈局,但是您能否更詳細地解釋網格佈局佈局?我確實閱讀了oracle網站教程。我只問,因爲這對我來說並不是很清楚。也許以對話的形式提供一點反饋會更好。 – user3015246

+0

也許是一個代碼的例子,它只使用gridbag佈局中的一個按鈕,並將按鈕放在網格大小(10,10)的單元格(4,3)中? – user3015246

+0

非常感謝您的編輯。我認爲這個程序創建了一個100個面板,然後在(4,3)的面板上放置一個按鈕。但是,只有一個或兩個組件有沒有辦法工作?你提到了一些關於間隔技巧。我想要使​​用少量組件並直接操作它們的原因是因爲我想從f JVM的角度思考並瞭解c.gridx,c.gridy,c.weightx等的工作原理。爲什麼他們都被置於中心位置。你的面板(0,0)怎麼能夠坐在左上角,但是即使gridx = 0,我的座標也被放在中心位置? – user3015246