2012-10-09 152 views
5

我試圖在Java中使用網格包佈局調整網格包佈局

public static void addComponentsToPane(Container pane) { 
     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     JLabel label1,label2,label3,result,title; 
     JButton calculate_btn; 
     JTextField side1,side2,side3; 

    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    if (shouldFill) { 
    //natural height, maximum width 
    c.fill = GridBagConstraints.HORIZONTAL; 
    } 


     title = new JLabel("Area of Triangle"); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 2; 
    c.gridy = -1; 
    pane.add(title, c); 



    label1 = new JLabel("Side 1: "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 1; 
    c.gridy = 1; 
    pane.add(label1, c); 

     label2 = new JLabel("Side 2: "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 1; 
    c.gridy = 2; 
    pane.add(label2, c); 


     label3 = new JLabel("Side 3: "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 1; 
    c.gridy = 3; 
    pane.add(label3, c); 

     side1 = new JTextField(" "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 2; 
    c.gridy = 1; 
    pane.add(side1, c); 

     side2 = new JTextField("Side 3: "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 2; 
    c.gridy = 2; 
    pane.add(side2, c); 

     side3 = new JTextField("Side 3: "); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 2; 
    c.gridy = 3; 
    pane.add(side3, c); 

    calculate_btn = new JButton("Calculate"); 
    //c.fill = GridBagConstraints.HORIZONTAL; 
    c.ipady = 30;  //make this component tall 
    c.weightx = 0.5; 
    c.gridwidth = 3; 
    c.gridx = 0; 
    c.gridy = 5; 
    pane.add(calculate_btn, c); 

     result = new JLabel("Result displayed here"); 
    if (shouldWeightX) { 
    c.weightx = 0.5; 
    } 
    c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20; 
    c.gridx = 2; 
    c.gridy = 7; 
    pane.add(result, c); 


    } 

所以上面的代碼基本上只是將被添加到一個GUI組件來實現這種佈局,但我不是很讓我想,這就是我想實現

enter image description here

但是,這就是我與上面的代碼

獲取

所以,當我編譯上面就是我endup用,還可能的話,我不希望用戶調整窗口的大小,我猜一些布爾與窗口的屬性之一..

+0

'pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);'這行代碼是由'達里爾Burke'或'camickr',特別是通過提及搖擺大師的第二已經得到在這個論壇上關於GBC的幾個例子, – mKorbel

+0

只有在所有列都被填充(使用不可見的JComponents)的情況下,GBC才能正確工作,那麼你可以使用任何列的任何一列:-), – mKorbel

+0

+1在手邊佈置'GridBagLayout' – MadProgrammer

回答

3

問題是,您正在設置ipady,這會「垂直」伸展您的組件。您可能正在尋找insets屬性:http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagConstraints.html#insets

嘗試使用這樣的:

c.insets = new Insets(10, 0, 10, 0); 
+0

是的,就是這樣。 – Zarkonnen

+0

程序初始化時默認的大小是多少?它是非常小,我想這個固定。 – noobprogrammer

+0

此外,應用程序的標題如何推動它到頂部? – noobprogrammer

2

一個問題我看到你的代碼是你正在重新使用相同的GridBagConstraints對象來添加所有的元素,這是不推薦的。

我的建議是使用像NetBeans或Eclipse中可用的GUI構建器。在Java中手動編碼GUI非常痛苦,特別是GridBagLayout非常適合在生成的佈局代碼中使用。

或者,使用類似TableLayout的東西 - 或咬住子彈和read up的錯綜複雜的GridBagConstraints

+3

重複使用相同的GBC不是問題。它看起來很醜,但這不是問題。 –

3

下面是使用這會導致GridBagLayout另一種方法......

enter image description here enter image description here

public class TestLayout { 

    public static void main(String[] args) { 

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

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new FormPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    protected static class FormPane extends JPanel { 

     JLabel label1, label2, label3, result, title; 
     JButton calculate_btn; 
     JTextField side1, side2, side3; 

     public FormPane() { 
      // You may not need this, I needed it because the window packed to 
      // small on my mac ;)    
      setBorder(new EmptyBorder(4, 4, 4, 4)); 

      setLayout(new GridBagLayout()); 
      GridBagConstraints c = new GridBagConstraints(); 

      title = new JLabel("Area of Triangle"); 
      label1 = new JLabel("Side 1: "); 
      label2 = new JLabel("Side 2: "); 
      label3 = new JLabel("Side 3: "); 
      side1 = new JTextField(4); 
      side2 = new JTextField(4); 
      side3 = new JTextField(4); 
      calculate_btn = new JButton("Calculate"); 

      result = new JLabel("Result displayed here"); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 
      gbc.anchor = GridBagConstraints.NORTH; 
      gbc.gridwidth = 2; 
      gbc.weighty = 1; 
      add(title, gbc); 

      gbc.weighty = 0; 
      gbc.gridy = 1; 
      gbc.anchor = GridBagConstraints.WEST; 
      gbc.gridwidth = 1; 
      add(label1, gbc); 
      gbc.gridy++; 
      add(label2, gbc); 
      gbc.gridy++; 
      add(label3, gbc); 

      gbc.gridy = 1; 
      gbc.gridx = 1; 
      add(side1, gbc); 
      gbc.gridy++; 
      add(side2, gbc); 
      gbc.gridy++; 
      add(side3, gbc); 

      gbc.gridx = 0; 
      gbc.gridwidth = 2; 
      gbc.gridy++; 
      add(result, gbc); 

      gbc.gridy++; 
      gbc.weighty = 1; 
      gbc.anchor = GridBagConstraints.NORTH; 
      add(calculate_btn, gbc); 

     } 

    } 

} 

如果您想在標題和字段之間添加空格, d some Insets

gbc.insets = new Insets(0, 0, 8, 0); 
add(title, gbc); 

// Don't forget to reset them ;) 
gbc.insets = new Insets(0, 0, 0, 0); 

我剛剛意識到結果應該顯示在按鈕下方。簡單交換線路add(result, gbc)add(calculate_btn, gbc),一切都應該保持相同

+0

EXCELLENT!有沒有什麼方法可以設置JFRAME的大小並用它來鎖定它? – noobprogrammer

+0

JFrame.setSize,但要小心,你在一個可變的環境中工作,字體大小,DPI和分辨率從不相同,最好使用pack來允許框架調整到其內容的首選大小。一個空的寄宿生到我的許多框架,給它一些圍繞內容的空間。要停止用戶調整框架的大小,請使用JFrame.setResizable – MadProgrammer