Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何防止JButton改变大小?_Java_Swing_Jbutton_Layout Manager_Grid Layout - Fatal编程技术网

Java 如何防止JButton改变大小?

Java 如何防止JButton改变大小?,java,swing,jbutton,layout-manager,grid-layout,Java,Swing,Jbutton,Layout Manager,Grid Layout,我想做一个简单的游戏,你可以用瓷砖来做凯尔特人的设计,但我似乎不能让按钮保持相同的大小。代码如下: public class CTGContent { public static JPanel content = new JPanel(); private static JButton board[][] = new JButton[20][20]; static private Random random = new Random(); public st

我想做一个简单的游戏,你可以用瓷砖来做凯尔特人的设计,但我似乎不能让按钮保持相同的大小。代码如下:

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();
            }
        });
    }
}
公共类CTG内容{
public static JPanel content=new JPanel();
专用静态JButton板[][]=新JButton[20][20];
静态私有随机=新随机();
公共静态int-CTGcolumn=random.nextInt(14)+1;
公共静态int-CTGRow=15;
私有静态网格布局;
公共最终整型getBoardWidth(){
返回CTG列*40;
}
公共最终int getBoardHeight(){
返回CTGRow*40;
}
公共静态无效初始化板(){
int行=0;
int列=0;
int defaultCOL=0;
int defaultROW=0;
boardLayout=新网格布局(CTGRow、CTGcolumn);
content.setLayout(boardLayout);
随机决定=新随机();
while(行
它通常以正确的大小开始,但由于某种原因,如果我调整窗口大小或其他事情,他们会随之改变大小。我需要他们保持不变的大小,无论什么。我如何才能做到这一点?

更改:

window.setContentPane(content);
致:

还有其他方法可以做到这一点,比如将布局设为
FlowLayout
,但是GBL将使其在垂直和水平方向上都保持良好的居中状态

提示
  • 请学习类、方法和属性名称的通用(特别是用于名称的大小写),并一致使用它们
  • 为了更快地获得更好的帮助,请发布一个。该代码只需要导入就可以成为MCVE
  • 另见

  • 您使用
    GridLayout
    ,因为您的按钮总是水平/垂直调整大小,以填充整个网格单元。例如,为了防止出现这种情况,您可以使用另一个
    布局管理器


    始终阅读。

    请对代码、输入/输出和结构化文档(如HTML或XML)使用代码格式。要做到这一点,请选择示例并单击消息发布/编辑表单上方的
    {}
    按钮。请注意,链接的“副本”绝对不是。链接的问题是让一组按钮彼此保持相同的大小,答案是
    GridLayout
    。这个问题已经使用了
    GridLayout
    ,但希望在调整窗口大小时保持按钮的大小不变,答案是将整个面板放入一个父面板中,该面板考虑内容的大小,不会调整其大小@alesc3我没有否决你的答案,如果你愿意,我会证明。实际上没有必要更改
    GridLayout
    的布局。只需将整个面板约束在
    GridBagLayout
    中即可。@AndrewThompson,哦,真的,你是对的。主题启动者可以将其面板包装为不可调整大小的LayoutManager。谢谢Andrew Thompson。这帮了大忙:D
    JPanel contentCenter = new JPanel(new GridBagLayout());
    contentCenter.add(content);
    window.setContentPane(contentCenter);