2012-11-06 54 views
4

這是我在這裏的第一個問題,所以請原諒我,當我違反任何規則,或者不使用的Java:GridBagLayout的混亂

我創造的Java Swing一個簡單的形式,它由1的JLabel的正確的格式,1周的JTextField和1個按鈕

|---------------------------| 
|       | 
|   JLabel   | 
|       | 
|---------------------------| 
| JTextField | Button | 
|---------------------------| 

的按鈕應該在右下角,JTextField的離開它,選擇JLabel頂部,跨越兩列

我想該按鈕被一個固定的大小,JTextField固定的高度,但使用完整的widt h(按鈕使用的除外)和JLabel使用所有其他空間(使用和高度)

我甚至不確定是否應該使用GridBagLayout或另一個佈局?

這可能是一個非常簡單的問題,但讓我困惑了很長一段時間(與GridBarLayout我想太多的選擇)

回答

1

OK是一個演示代碼,應該讓你去:

import java.awt.Color; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class TestGridBagLayout { 

    protected void initUI() { 
     JFrame frame = new JFrame(TestGridBagLayout.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JButton button = new JButton("A button"); 
     JTextField textField = new JTextField(); 
     JLabel label = new JLabel("A cool long nice label that will stretch."); 
     label.setHorizontalAlignment(JLabel.CENTER); 
     label.setBorder(BorderFactory.createLineBorder(Color.GREEN)); 
     JPanel panel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.fill = GridBagConstraints.BOTH;// Fill the "cell" in both direction 
     gbc.weightx = 1.0;// Allocate extra-width to the label 
     gbc.weighty = 1.0;// Allocate extra-height to the label 
     gbc.gridwidth = GridBagConstraints.REMAINDER;// The label takes all the available width of the "row" 
     panel.add(label, gbc); 

     gbc.weighty = 0; // Don't stretch TF vertically 
     gbc.fill = GridBagConstraints.HORIZONTAL; // Fill horizontally 
     gbc.gridwidth = GridBagConstraints.RELATIVE; 
     panel.add(textField, gbc); 
     gbc.weightx = 0; // No extra horizontal space is given to the button 
     gbc.fill = GridBagConstraints.NONE; // No fill for the button 
     panel.add(button, gbc); 
     frame.add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestGridBagLayout().initUI(); 
      } 
     }); 
    } 

} 
+0

謝謝!這完美的作品! (它只顯示了一些空格在textfield下方,但是改變它的填充以解決這個問題..謝謝! – Hrqls

2

首先,您面板的佈局設置爲GridBagLayout

然後,創建一個GridBagConstraints對象並將填充設置爲GridBagConstraints.BOTH

對於JLabel,請在約束對象上設置以下屬性:gridx = 0, gridy = 0, gridwidth = 2, gridheight = 2, weightx = 1, weighty = 1

對於JTextField,請在約束對象上設置以下屬性:gridx = 0, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 1, weighty = 0

對於JButton,請在約束對象上設置以下屬性:gridx = 1, gridy = 1, gridwidth = 1, gridheight = 1, weightx = 0, weighty = 0

+0

謝謝你的答案! 與GridBagConstraints的所有組件都顯示在同一行(位於屏幕中心) [空格] [JTextField中] [JLabel的] [空格] [按鈕] – Hrqls

+0

我想我應該用gridwidth.REMAINDER和gridwidth。相對....但我嘗試了很多組合,它總是把事情弄糟:)它似乎與2個面板的BorderLayout可能是去..或2個面板的BorderLayout和底部的GridBagLayout(南)面板..雖然這似乎相當複雜和過度的東西,應該是很簡單的? – Hrqls

+0

@Hrqls我添加了一個如何使用GBL和RELATIVE/REMAINDER的例子。 –

2

類BorderLayout易於使用,比GridBagLayout功能弱。

但是當事情很簡單時,解決方案必須相同。這裏

panel.add(label, BorderLayout.CENTER); 
JPanel south = new JPanel(); 
south.add(textfield); 
south.add(button); 
button.setPreferredSize(x, y); 
panel.add(south, BorderLayout.SOUTH); 
+0

調用setPreferredSize總是在問題的中間運行。 –

+0

OP說:「我希望Button是一個固定大小」 – Aubin

+0

這不是因爲您沒有設置大小隨時間變化的首選大小(這主要取決於所選佈局管理器及其約束)。只是不要設置首選大小。 –

1

我不知道這是做一個平常的事情,但下面是其工作代碼(主要是代碼丹和紀堯姆)

//show stuff 
    setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 
    //show label 
    c.fill = GridBagConstraints.BOTH;   // Fill the "cell" in both direction 
    c.weightx = 1.0;       // Allocate extra-width to the label 
    c.weighty = 1.0;       // Allocate extra-height to the label 
    c.gridwidth = GridBagConstraints.REMAINDER; // The label takes all the available width of the "row" 
    add(mlblShow,c); 
    //show cmd txt 
    c.weighty = 0;        // Don't stretch TF vertically 
c.fill = GridBagConstraints.BOTH;   // Fill horizontally and vertically 
c.gridwidth = GridBagConstraints.RELATIVE; 
    add(mtxtCmd,c); 
    //show send button 
    c.weightx = 0;        // No extra horizontal space is given to the button 
c.fill = GridBagConstraints.NONE;   // No fill for the button 
    add(cmdSend,c);