gpt4 book ai didi

java - 如何重置 JPanel?

转载 作者:行者123 更新时间:2023-12-02 04:25:52 24 4
gpt4 key购买 nike

我有一个简单的乒乓球游戏,当用户单击 JPanel 上显示的 JButton 时,它应该重置游戏。我怎样才能做到这一点?我想删除 JPanel 并添加一个新的(JPanel 包含游戏所需的所有代码/类引用),但是我尝试编写此内容,并且它没用,什么也没有发生。这是我的代码:

JFrame 类:

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();
}
}

JPanel 类:

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 类(我的错误是没有提及这一点),但尝试在第一次“提及它们时将 gameOver false 设置为 false “(我不知道正确的术语,只需执行 static boolean gameOver = false; 而不是 static boolean gameOver;)。在所有其他类(class)中也这样做。很抱歉没有说出您必须更改哪些变量,但我不会说任何我不能百分百确定但无法测试的内容:P(也许更有经验的人可以为您提供更多帮助)

关于java - 如何重置 JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189733/

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