gpt4 book ai didi

java - Java Swing 游戏中重置倒计时器不起作用 + JDialog/JOptionPane

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:40 25 4
gpt4 key购买 nike

我不太擅长 Java Swing,我正在尝试使用计时器延迟 3 秒启动游戏

但同时我想显示一个对话框(而且游戏必须等待 3 秒,因此焦点需要在对话框上)

所以我的对话框如下:got this sample code

所以在我的游戏面板中我这样做:

public class GamePlayPanel extends JPanel implements ActionListener {
// attributes
private JOptionCountDownTimer countDownDialog;
public GamePlayPanel(MainWindow mainWindow) {
// initialization attributes
initLayoutPanel();
this.timer = new Timer(DELAY, this);
// Added a delay of 3 seconds so you can prepare to for the game
this.timer.setInitialDelay(3000);
resetTime();
}


public void startGame() {
this.gamePanel.requestFocus();
this.countDownDialog.startCountDown();
startTimer(); // this is my game timer to record the game time
}


public void restartGame() {
this.countDownDialog.resetCountDown();
startTimer();
this.gamePanel.requestFocus();
}
}

它工作正常,但如果我重新启动游戏,倒计时器将从 0 -> 2 秒开始。

关于我的类JOptionCountDownTimer还有什么更好的想法吗?我试图让它扩展一个 JDialog 类,但我无法让它工作。

最佳答案

试试这个,看看它是否适合你。您只需获取对话框类代码即可。您需要做的就是将父框架传递给它,这对于您想要的模态和秒数都是正确的。您可能还想把它美​​化一下。我只是提供功能

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

public class CountDownTimer {
public CountDownTimer() {
final JFrame frame = new JFrame();
JButton button = new JButton("Open Dilaog");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new CountDownTimerDialog(frame, true, 5);
}
});

frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private class CountDownTimerDialog extends JDialog {
private int count;

public CountDownTimerDialog(JFrame parent, boolean modal, int seconds) {
super(parent, modal);
count = seconds;
final JLabel countLabel = new JLabel(String.valueOf(seconds), JLabel.CENTER);
countLabel.setFont(new Font("impact", Font.PLAIN, 36));
JLabel message = new JLabel("Wait to Start Game");
message.setFont(new Font("verdana", Font.BOLD, 20));

JPanel wrapper = new JPanel(new BorderLayout());
wrapper.setBorder(new EmptyBorder(10, 10, 10, 10));
wrapper.add(countLabel);
wrapper.add(message, BorderLayout.SOUTH);
add(wrapper);

Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (count == -1) {
dispose();
} else {
countLabel.setText(String.valueOf(count));
count--;
}
}
});
timer.setInitialDelay(0);
timer.start();

pack();
setLocationRelativeTo(parent);
setVisible(true);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new CountDownTimer();
}
});
}
}

关于java - Java Swing 游戏中重置倒计时器不起作用 + JDialog/JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22911611/

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