java - 如何防止 JButton 改变大小?

标签 java swing jbutton layout-manager grid-layout

我正在尝试制作一个简单的游戏,您可以在其中设置构成凯尔特设计的图 block ,但我似乎无法让按钮保持相同的大小。这是代码:

public class CTGContent {

    public static JPanel content = new JPanel();
    private static JButton board[][] = new JButton[20][20];

    static private Random random = new Random();

    public static int CTGcolumn = random.nextInt(14)+1;
    public static int CTGRow = 15;
    private static GridLayout boardLayout;

    public final int getBoardWidth(){
        return CTGcolumn*40;
    }

    public final int getBoardHeight(){
        return CTGRow*40;
    }

    public static void initializeBoard(){
        int row = 0;
        int column = 0;

        int defaultCOL = 0;
        int defaultROW = 0;

        boardLayout = new GridLayout(CTGRow, CTGcolumn);
        content.setLayout(boardLayout);

        Random determine = new Random(); 

        while (row < CTGRow){
            column = 0;
            while (column < CTGcolumn){
                board[row][column] = new JButton(new ImageIcon("images\\template.gif"));
                board[row][column].setPreferredSize(new Dimension(40, 40));
                board[row][column].setMaximumSize(new Dimension(40, 40));
                board[row][column].setMinimumSize(new Dimension(40, 40));
                content.add(board[row][column]);
                column++;
            }
            row++;
        }
    }

    private static void build(){

        initializeBoard();

        JFrame window = new JFrame("testing the menubar");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(content);
        content.add(new JLabel("My Label"));
        window.pack();
        window.setVisible(true);    

    }

    public CTGContent() {

    }

    public CTGContent(JFrame window){

        initializeBoard();
        window.add(content);

    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                    build();
            }
        });
    }
}

它通常从正确的大小开始,但由于某种原因,如果我调整窗口或其他东西的大小,它们会随之改变大小。无论如何,我都需要它们保持相同的尺寸。我该怎么做?

最佳答案

改变:

window.setContentPane(content);

收件人:

JPanel contentCenter = new JPanel(new GridBagLayout());
contentCenter.add(content);
window.setContentPane(contentCenter);

enter image description here

enter image description here

还有其他方法可以做到这一点,比如将布局设为 FlowLayout,但 GBL 会使其很好地居中 - 无论是垂直还是水平。

提示

  1. 请学习常用Java naming conventions (特别是用于名称的大小写)用于类、方法和属性名称并始终如一地使用它们。
  2. 为了尽快获得更好的帮助,请发布 MCVE .该代码只需导入即可使其成为 MCVE。
  3. 另请参阅 this answerCentering a JLabel on a JPanel

关于java - 如何防止 JButton 改变大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21228740/

相关文章:

java - JFrame 周围的 LineBorder 消失

java - 尝试在 JTextArea 中使用 BufferedWriter 时发生并发冲突

java - JButton 不会显示背景颜色

java - 金钱累积按钮

java - 具有多个 targetEntity 的 Hibernate 映射接口(interface)

Mac OS X 10.5.8 上的 Java SE6 32 位

Java:KeyListener 不工作

java - 使多个 JTextFields 改变颜色 Java

java - manytomany 检索条件

Java 按钮 : adding an ActionListener