gpt4 book ai didi

java - pack() 无法正确打包

转载 作者:行者123 更新时间:2023-12-02 07:38:00 24 4
gpt4 key购买 nike

我这里有一个 JFrame:

import javax.swing.JFrame;

public class Game extends JFrame {
private static final long serialVersionUID = -7919358146481096788L;
Arena a = new Arena();
public static void main(String[] args) {
new Game();
}
private Game() {
setTitle("Insert name of game here");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(a);
pack();
setResizable(false);
setVisible(true);
}
private void refresh() {

}
}

Arena 是一个 JPanel:

import java.awt.Dimension;

import javax.swing.JPanel;

public class Arena extends JPanel {
private static final long serialVersionUID = 6499922741928110264L;
byte[][] world = new byte[6000][2000];
int blockPix = 20;
int x = 2999 * blockPix, y = 999 * blockPix;
public Arena() {
setLayout(null);
setMinimumSize(new Dimension(600, 600));
setMaximumSize(new Dimension(600, 600));
setPreferredSize(new Dimension(600, 600));
}
}

然而 JFrame 的大小最终变成了 0 x 0!

最佳答案

您应该删除 Game 构造函数中的指令 setLayout(null);

关于java - pack() 无法正确打包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com