2012-11-06 55 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);