0

我有 2 个按钮,一个已命名btnShort,一个已命名btnLong。我希望它们具有相同的宽度,那么最好的选择是什么?

在此处输入图像描述

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TestCode2 {

public static void main(String[] args) {

    JFrame window = new JFrame("Test2");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(400, 200);

    // ---------------------------------------------------------------------

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();

    JPanel container = new JPanel(layout);
    window.add(container);

    constraints.gridy = 0;

    JButton btnShort = new JButton("Short");
    layout.setConstraints(btnShort, constraints);
    container.add(btnShort);

    constraints.gridy = 1;

    JButton btnLong = new JButton("That's a long button");
    layout.setConstraints(btnLong, constraints);
    container.add(btnLong);

    //This one won't work because button dimension is not known yet...
    //btnShort.setPreferredSize(new Dimension(btnLong.getWidth(), btnLong.getHeight()));

    // ---------------------------------------------------------------------

    window.setVisible(true);
}

}
4

2 回答 2

3

GridBagConstraints填充属性设置为,GridBagConstraints.HORIZONTAL使两个JButton组件在容器中占据相等的宽度

constraints.fill = GridBagConstraints.HORIZONTAL;
于 2013-07-05T09:37:46.447 回答
1

使用适当的布局管理器,比如GridBagLayout它的约束......

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Buttons {

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

    public Buttons() {
        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("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(new JButton("I'm a very long button"), gbc);
            add(new JButton("I'm not"), gbc);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }    
}
于 2013-07-05T09:38:59.217 回答