使用GridBagLayout时设置JButton大小

时间:2013-12-01 16:50:28

标签: java swing jbutton jtextfield gridbaglayout

我有这个简单的JFrame,我想知道如何设置按钮的高度以准确匹配文本字段的高度。 为什么插图不能在按钮的右侧工作?按钮和窗口边框之间没有填充。

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {
    public static void main(String[] args) {
        new MyFrame();
    }

    public MyFrame() {
        super("My Frame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        GridBagConstraints c;
        final Insets insets = new Insets(4, 4, 4, 4);

        JLabel dbLocationLabel = new JLabel("Database file:");
        c = new GridBagConstraints();
        c.insets = insets;
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_START;
        this.add(dbLocationLabel, c);

        JTextField dbLocationText = new JTextField();
        c = new GridBagConstraints();
        c.insets = insets;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridwidth = 5;
        c.gridy = 0;
        c.weightx = 0.1;
        this.add(dbLocationText, c);

        JButton dbLocationButton = new JButton("...");
        c = new GridBagConstraints();
        c.insets = insets;
        c.gridx = 2;
        c.gridy = 0;
        this.add(dbLocationButton);

        this.setSize(500, 150);
        this.setLocation(300, 400);
        this.setVisible(true);
    }
}

2 个答案:

答案 0 :(得分:4)

1)您应始终拥有主要的JPanel。 从不将组件直接放在JFrame上。你将为自己节省很多未来的悲伤。

2)你忘记了对JButton add的约束。

3)我将JTextField上的填充更改为BOTH,以使JTextField更高。

这是更改后的代码。

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {
    private static final long   serialVersionUID    = 1L;

    public static void main(String[] args) {
        new MyFrame();
    }

    public MyFrame() {
        super("My Frame");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());

        GridBagConstraints c;
        final Insets insets = new Insets(4, 4, 4, 4);
        final Insets buttonInsets = new Insets(4, 4, 4, 8);

        JLabel dbLocationLabel = new JLabel("Database file:");
        c = new GridBagConstraints();
        c.insets = insets;
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_START;
        mainPanel.add(dbLocationLabel, c);

        JTextField dbLocationText = new JTextField();
        c = new GridBagConstraints();
        c.insets = insets;
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 1;
//        c.gridwidth = 5;
        c.gridy = 0;
        c.weightx = 0.1;
        mainPanel.add(dbLocationText, c);

        JButton dbLocationButton = new JButton("...");
        c = new GridBagConstraints();
        c.insets = buttonInsets;
        c.gridx = 2;
        c.gridy = 0;
        mainPanel.add(dbLocationButton, c);

        this.add(mainPanel);
        this.setSize(500, 150);
        this.setLocation(300, 400);
        this.setVisible(true);
    }
}

答案 1 :(得分:2)

SSCCE:

package stack;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SameHeight extends JFrame {

    JTextField jtf;
    JButton btn;

    public SameHeight(){
        GridBagConstraints gbc = new GridBagConstraints();
        jtf = new JTextField(30);
        btn = new JButton("Click Me");

        getContentPane().setLayout(new GridBagLayout());
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 7;
        gbc.gridheight = 3;
        gbc.fill = GridBagConstraints.BOTH;

        getContentPane().add(jtf,gbc);
        gbc.gridx = 8;
        getContentPane().add(btn,gbc);

        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

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

            @Override
            public void run() {
                new SameHeight();

            }
        });
    }
}  

<强>输出:
enter image description here

<强>解释
确保您的JTextFieldJButton具有相同的gridheight。另外,将fill的{​​{1}}设置为GridBagConstraints。这样,您的组件将拉伸以适应宽度和高度。