简单的JFrame没有显示出来

时间:2014-01-15 12:15:40

标签: java eclipse swing jframe

我几乎完全复制了源代码,但我的工作不起作用。什么都没有显示出来。我做错了什么?无论我在代码中删除了多少内容,我都无法显示简单的JFrame。这可能是我在Eclipse中做过的事情吗?

public class Game extends Canvas {

    private JPanel mainPanel;
    private JFrame mainFrame;
    private BufferStrategy strategy;

    public Game(){

        JFrame mainWindow = new JFrame("Canvas Game");
        JPanel mainPanel = (JPanel) mainWindow.getContentPane();
        mainPanel.setPreferredSize(new Dimension(500,800));
        mainPanel.setLayout(null);
        mainWindow.setResizable(false);

        setIgnoreRepaint(true);

        setBounds(0, 0, 500, 800);
        mainPanel.add(this);

        createBufferStrategy(2);
        strategy = getBufferStrategy();

        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0,0,500,800);

        strategy.show();

        mainFrame.setVisible(true);
        mainFrame.pack();       
    }

    public static void main(String[] args){
        Game myGame = new Game();
    }

}

1 个答案:

答案 0 :(得分:0)

您似乎错过了某处mainWindow.show()

一句话:知道构造函数中调用的方法的作用应该很好。

相关问题