如何以及在何处使用javax.swing计时器
请帮助,我正在尝试在我的程序中使用javax.swing.Timer。我看过很多例子,但对我来说仍然没有意义。我正在编写一个程序,让用户猜测价格。我似乎不明白的是,在点击“新游戏”按钮后,如何让计时器从30秒开始倒计时。如果用户没有猜到正确答案,那么我希望游戏显示“你输了”,但如果他们在30秒内得到正确答案并显示时间,我也希望计时器停止。我想我应该用如何以及在何处使用javax.swing计时器,java,swing,timer,Java,Swing,Timer,请帮助,我正在尝试在我的程序中使用javax.swing.Timer。我看过很多例子,但对我来说仍然没有意义。我正在编写一个程序,让用户猜测价格。我似乎不明白的是,在点击“新游戏”按钮后,如何让计时器从30秒开始倒计时。如果用户没有猜到正确答案,那么我希望游戏显示“你输了”,但如果他们在30秒内得到正确答案并显示时间,我也希望计时器停止。我想我应该用 timer = new Timer(Speed, this); timer.start(); timer.end(); 但是,我不
timer = new Timer(Speed, this);
timer.start();
timer.end();
但是,我不确定计时器还需要什么,或者在代码中的什么位置放置这些。任何帮助都将不胜感激。下面是我的程序代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class ClockGame extends JFrame {
//Declare fields for GUI components
private JTextField guessField;
private JButton newGameButton;
private JLabel messageLabel;
private JLabel guessLabel;
private ImageIcon clockImage;
private int countTotal;
private Random rand;
private JLabel title;
private int number;
private Timer timer;
public ClockGame() {
//Build GUI
super ("Clock Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set layout
this.setLayout(new BorderLayout());
//Create the main panel
JPanel mainPanel = new JPanel();
//Create components and place them in the panel
rand = new Random();
guessLabel = new JLabel("Guess: ");
guessField = new JTextField(20);
messageLabel = new JLabel(" Click New Game to Begin");
clockImage = new ImageIcon("clock.jpg");
newGameButton = new JButton("New Game");
title = new JLabel("The Clock Game", clockImage, SwingConstants.CENTER);
//Set font for clockGameLabel
title.setFont(new Font("Calibri", Font.BOLD, 24));
//Set messageLabel Color
messageLabel.setOpaque(true);
messageLabel.setBackground(Color.yellow);
newGameButton.addActionListener(new ButtonListener());
guessField.addActionListener(new AnswerListener());
//Add components to the panel
mainPanel.add(guessLabel);
mainPanel.add(guessField);
mainPanel.add(newGameButton);
this.add(title, BorderLayout.NORTH);
this.add(messageLabel, BorderLayout.SOUTH);
//Add the panel to this JFrame
this.add(mainPanel, BorderLayout.CENTER);
//Sizes this JFrame so that it is just big enough to hold the components
this.setSize(340,225);
//Make the JFrame visible on the screen
this.setVisible(true);
}
private class AnswerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Code to check to see if answer is correct
int sum = number;
int answer = Integer.parseInt(guessField.getText());
Color purple = new Color(153, 153, 253);
countTotal++;
if (sum < answer)
{
messageLabel.setText("Too High");
messageLabel.setBackground(Color.red);
}
else if (sum > answer)
{
messageLabel.setText("Too Low");
messageLabel.setBackground(purple);
}
else
{
messageLabel.setText("Correct! It took you " + countTotal + " tries, in " +
timer + " seconds");
messageLabel.setBackground(Color.yellow);
}
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
number = rand.nextInt(1001);
messageLabel.setText(" The price is between $1 and $1000, begin.");
messageLabel.setBackground(Color.green);
countTotal = 0;
}
}
public static void main(String[] args) {
ClockGame frame = new ClockGame();
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.Random;
公共类ClockGame扩展JFrame{
//为GUI组件声明字段
私有JTextField猜测字段;
私有JButton newGameButton;
私有JLabel消息标签;
私人JLabel标签;
私有图像;
私人整数总数;
私有随机兰德;
私人产权;
私有整数;
私人定时器;
公共游戏(){
//构建GUI
超级(“时钟游戏”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//集合布局
此.setLayout(新的BorderLayout());
//创建主面板
JPanel mainPanel=新的JPanel();
//创建零部件并将其放置在面板中
rand=新随机数();
猜标签=新的JLabel(“猜:”);
猜测字段=新的JTextField(20);
messageLabel=newjlabel(“单击新建游戏开始”);
clockImage=新图像图标(“clock.jpg”);
newGameButton=新JButton(“新游戏”);
标题=新的JLabel(“时钟游戏”,时钟图像,SwingConstants.CENTER);
//设置clockGameLabel的字体
title.setFont(新字体(“Calibri”,Font.BOLD,24));
//设置消息标签颜色
messageLabel.set不透明(true);
messageLabel.setBackground(颜色:黄色);
addActionListener(newButtonListener());
addActionListener(新的AnswerListener());
//将零部件添加到面板中
主面板。添加(标签);
主面板。添加(猜测字段);
主面板。添加(新游戏按钮);
添加(标题,BorderLayout.NORTH);
添加(messageLabel,BorderLayout.SOUTH);
//将面板添加到此框架
添加(主面板,BorderLayout.CENTER);
//调整此JFrame的大小,使其刚好能够容纳组件
这个。设置大小(340225);
//使JFrame在屏幕上可见
此.setVisible(true);
}
私有类AnswerListener实现ActionListener{
已执行的公共无效操作(操作事件e){
//检查答案是否正确的代码
整数和=数字;
int answer=Integer.parseInt(guessField.getText());
紫色=新颜色(153153253);
countTotal++;
如果(总和<答案)
{
messageLabel.setText(“太高”);
messageLabel.setBackground(颜色:红色);
}
否则如果(总和>答案)
{
messageLabel.setText(“太低”);
messageLabel.setBackground(紫色);
}
其他的
{
messageLabel.setText(“正确!您尝试了”+countTotal+“次,次”+
计时器+“秒”);
messageLabel.setBackground(颜色:黄色);
}
}
}
私有类ButtonListener实现ActionListener{
已执行的公共无效操作(操作事件e){
编号=兰特·耐克斯汀(1001);
setText(“价格在1美元到1000美元之间,begin.”);
messageLabel.setBackground(颜色为绿色);
countTotal=0;
}
}
公共静态void main(字符串[]args){
ClockGame帧=新的ClockGame();
}
}
没有end()
方法。首先,您需要阅读API以使用适当的方法
单击“新游戏”按钮后从30秒开始倒计时
因此,在ActionListener
中,您需要添加一个按钮来启动计时器,并安排它每秒钟启动一次。当计时器启动时,您将计数减少1
如果他们得到正确答案,我也希望计时器停止
所以当他们得到正确答案时,你就停止计时。因此,在更新标签文本的代码中,停止计时器
如果用户没有猜到正确答案,那么我希望游戏显示“你输了”
因此,当计时器计数达到0时,1)停止计时器,2)显示消息
在类的构造函数中,您将实际创建计时器,以便类中的上述方法具有对计时器的引用,以便可以根据需要启动和停止计时器
你真正需要做的是忘记你的游戏,学习如何使用计时器。因此,您可以创建一个带有标签和两个按钮的框架。标签将显示初始计数30。然后,您有一个“开始”按钮,每当计时器启动时,标签将递减1。然后你有一个“停止”按钮,停止计时器,这样计数就不会减少
一旦理解了启动和停止计时器的基本概念,就可以将代码添加到实际程序中 好的开始。。。您需要一个JLabel来打印“youlose”,另一个JLabel打印玩家回答问题所花的时间。按您的意愿将这些添加到您的框架中
JLabel outcome = new JLabel(); //setText to win or lose.
JLabel countdown = new JLabel(); //setTime as Timer counts down.
在实例化这些标签之后。计时器需要实例化
Timer timer = new Timer(1000, new ActionListener() { //Change parameters to your needs.
int count = 30;
public void actionPerformed(ActionEvent e) {
count--;
if(count == 0) //They lose
{
countdown.setText("CountDown: " + count);
outcome.setText("You Lose");
timer.stop(); //ends the countdown.
}
else if(userGotAnswer) // You need to create a boolean value that changes when a user gets the answer right.
{
countdown.setText("CountDown: " + count);
outcome.setText("You win");
timer.stop(); //ends the countdown
}
else
{
countdown.setText("CountDown: " + count); //default
}
}
});
然后打电话
timer.start();
当你想让计时器启动时。你根本不用这三行。尝试阅读此示例代码,您需要一个actionlistener,以便计时器在计数达到30并每秒递增1时运行
timer.start();