简体   繁体   中英

Simple JFrame not showing up

I almost literally copied source code and mine does not work. Nothing shows up at all. What did I do wrong? No matter how much stuff I delete in the code, I can't even get a simple JFrame to show up. Could it be something I did in 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();
    }

}

It seems that you are miising a mainWindow.show() somewhere.

One remark: it should be nice to know what the methods called in your constructor do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM