pack()无法正确打包

时间:2012-08-13 15:45:22

标签: java swing jframe jpanel layout-manager

我在这里有一个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比0!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

也许你可以setSize(600, 600);在你的构造函数中进行所有其他操作吗?