java - GridBagLayout 内按钮的大小

标签 java swing jbutton gridbaglayout preferredsize

我在 GridBagLayout 中有一个 3 * 6 的 JButtons 数组。但由于每个按钮的文本长度可能有所不同,因此每列按钮的宽度略有不同。如何使它们具有相同的宽度?

这是代码:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 6; j++)
    {
        buttonConstraints.fill = GridBagConstraints.BOTH;
        buttonConstraints.weightx = 1;
        buttonConstraints.weighty = 1;
        buttonConstraints.gridx = j;
        buttonConstraints.gridy = i;
        buttonPanel.add(buttons[i * 6 + j], buttonConstraints);
    }
}

提前致谢。

最佳答案

But since the text length of each button may vary, each column of buttons has slightly different width. How do I make them have the same width?

  • 简单的解决方案是使用GridLayout

  • GridBagLayout 创建列,每列的尺寸是根据第一行中每个 JButtonsPreferedSize 计算得出的,这些坐标用于当前容器中的其余行

编辑

enter image description here

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridFromGridBagLayout {

    private JFrame frame = new JFrame();
    private JPanel panel = new JPanel();

    public GridFromGridBagLayout() {
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 6; row++) {
            for (int col = 0; col < 3; col++) {
                JButton b = new JButton("(" + row + ", " + col + ")");
                b.putClientProperty("column", col);
                b.putClientProperty("row", row);
                b.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JButton btn = (JButton) e.getSource();
                        System.out.println("clicked column "
                                + btn.getClientProperty("column")
                                + ", row " + btn.getClientProperty("row"));
                    }
                });
                gbc.gridx = col;
                gbc.gridy = row;
                gbc.gridwidth = gbc.gridheight = 1;
                gbc.fill = GridBagConstraints.BOTH;
                gbc.anchor = GridBagConstraints.NORTHWEST;
                gbc.weightx = 33;
                gbc.weighty = 20;
                panel.add(b, gbc);
            }
        }
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GridFromGridBagLayout borderPanels = new GridFromGridBagLayout();
            }
        });
    }
}

关于java - GridBagLayout 内按钮的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16211917/

相关文章:

java - 如果传递的是 final 对象,是否仍应检查 null?

java - 如何在 JTable 的行中设置文本?

java - 在网格布局中,Jbutton 大小不会随着 java 中 setBounds() 的使用而改变

java - JButton、JTextField、JLabel 设置背景颜色不起作用

java - 如何在 MyEclipse 中定义 Label 类?

java - 如何在 Java 中编写全屏模式?

java - 如何让我的程序在 Java 窗口中运行?

Java-在类中找不到 Main 方法

java - 错误: "the method add(Component) in the type Container is not applicable for the arguments (Square)

java - 自动聚焦于 jpanel 上的 jButton