【问题标题】:I want my JFrame to adjust its dimensions and keep the buttons with constant size我希望我的 JFrame 调整其尺寸并保持按钮大小不变
【发布时间】:2014-09-26 10:49:20
【问题描述】:

我需要保持我的按钮大小不变,并重新调整 JFrame 的大小以适应按钮的大小,但我得到的只是 JFrame 上的大小不变,我的按钮会根据按钮的数量重新调整大小。

这是我的代码。

这是我创建按钮的地方,按钮的数量由收到的尺寸选择

[campoMinado.getAltura()][campoMinado.getLargura()]

      Height                Width

botoes[][] 是我的二维按钮数组。

for(int y = 0; y < campoMinado.getAltura(); y++){
    for(int x = 0; x < campoMinado.getLargura(); x++){
        botoes[x][y] = new BotaoCampoMinado(x, y);
        botoes[x][y].setPreferredSize(new Dimension(40,40));
        botoes[x][y].setMinimumSize(new Dimension(40,40));
        botoes[x][y].setMaximumSize(new Dimension(40,40));
        botoes[x][y].addActionListener(action);
        botoes[x][y].addMouseListener(mouseListener);
        getContentPane().add(botoes[x][y]);

     }
 }

【问题讨论】:

标签: java button resize jframe resizable


【解决方案1】:

创建 JFrame 时使用 .pack();

例如:

JFrame gameField = new JFrame();
gameField.pack();
gameField.setVisible(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多