如何重置 JPanel?

How to make a JPanel reset?

我有一个简单的乒乓球游戏,当用户单击 JPanel 上显示的 JButton 时,它应该重置游戏。我怎样才能做到这一点?我想只是删除 JPanel 并添加一个新的(JPanel 包含游戏所有必要的 code/class 参考)但是我尝试写这个,但它没有用, 什么都没发生。这是我的代码:

JFrame Class:

public class Window extends JFrame implements ActionListener {
    static int length = 1000;
    static int height = 1000;
    Display display = new Display();

    Window() {

        setTitle("Program Display");
        setSize(length + 22, height + 40);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JButton restart = new JButton("Start New Game");
        add(display);
        display.add(restart);
        restart.addActionListener(this);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        remove(display);
        Display display2 = new Display();
        JButton restart = new JButton("Start New Game");
        add(display2);
        display2.add(restart);
        restart.addActionListener(this);
        revalidate();
        repaint();
    }
}

J面板Class:

public class Display extends JPanel implements ActionListener {
    int up = 0;
    int down = 500;
    double ballx = 500;
    double bally = 500;
    char ballDirection;
    Rectangle border;
    static Rectangle borderEast;
    static Rectangle borderNorth;
    static Rectangle borderSouth;
    static Rectangle borderWest;
    static boolean gameOver;
    Timer timer;
    Paddle p;
    Ball b;

    Display() {
        p = new Paddle();
        b = new Ball();
        up = p.up;
        down = p.down;
        ballx =  b.ballx;
        bally =  b.bally;
        ballDirection = b.ballDirection;
        initTimer();
        b.startBall();
        addKeyListener(p);
        setFocusable(true);


    }


    public void initTimer() {
        timer = new Timer(10, this);
        timer.start();
    }

    public void setUpBorders(Graphics2D g2d) {
        border = new Rectangle(0, 0, Window.length, Window.height);
        borderEast = new Rectangle(Window.length, 0, 2, Window.height);
        borderWest = new Rectangle(0, 0, 2, Window.height);
        borderSouth = new Rectangle(0, Window.height, Window.length, 2);
        borderNorth = new Rectangle(0, 0, Window.length, 2);
        g2d.setColor(Color.RED);
        g2d.draw(border);

    }

    public void paintPaddle(Graphics2D g2d) {
        g2d.setColor(new Color(0, 130, 130));
        g2d.fill(p.paddle);

    }

    public void paintBall(Graphics2D g2d) {

        g2d.setColor(new Color(0, 130, 130));
        g2d.fillOval((int) ballx, (int) bally, 20, 20);

    }



    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.BLACK);
        Graphics2D g2d = (Graphics2D) g;
        setUpBorders(g2d);
        paintPaddle(g2d);
        paintBall(g2d);

        if(gameOver == true) {
                Font custom = new Font("Dialog", Font.BOLD, 60);
                g2d.setColor(Color.RED);
                g2d.setFont(custom);
                g2d.drawString("Game Over. Your score was: " + Ball.score + "!", 50, 500);

        }

    }




    public void checkBorderHit() {
        b.checkBorderHit();
        p.checkBorderHit();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        up = p.up;
        down = p.down;
        ballx =  b.ballx;
        bally =  b.bally;
        ballDirection = b.ballDirection;

        b.moveBall();
        checkBorderHit();
        repaint();


    }
}

你没说具体是什么不行,但是你忘了在加display2后调用revalidate()repaint()。如果您的问题是按下按钮后没有任何反应,这可能会解决它。

编辑:

我们仍然不能 运行 你编码,因为 Ball 和 Paddle class 不见了(我没有提到这一点是我的错误),但是尝试设置重要的变量,比如 gameOver false when first "mentioning them" (我不知道正确的术语,只是做 static boolean gameOver = false; 而不是 static boolean gameOver;)。在所有其他 classes 中也这样做。很抱歉没有说出你到底必须改变哪些变量,但我没有说任何我不能 100% 确定而不能测试它的事情:P(也许更有经验的人可以帮助你更多)