gpt4 book ai didi

Java 计时器程序不会更新显示的时间

转载 作者:行者123 更新时间:2023-12-01 09:45:41 28 4
gpt4 key购买 nike

我正在创建一个简单的java计时器,但是当单击“开始”按钮时,负责显示时间的JPanel不会更新。我使用 Swing 计时器来更新 JPanel,但没有效果。我是否在错误的组件上使用了它?这是我的代码...

JFrame(主定时器组件)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class theTimer extends JFrame{

//Initialize fields
private TimerPanel tp = new TimerPanel();
private JButton start,stop,reset;
private Dimension buttonSize = new Dimension(80,30);
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Main Method
public static void main(String[] args){
theTimer tT = new theTimer();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Constructor
public theTimer(){
setLayout(new FlowLayout());
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(tp);
addButtonAction();
setButtonSize();
add(start); add(stop); add(reset);
setTitle("Java Study Timer");
setVisible(true);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Set the size of the timer buttons
private void setButtonSize(){
start.setPreferredSize(buttonSize);
stop.setPreferredSize(buttonSize);
reset.setPreferredSize(buttonSize);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Gives the buttons functionality
private void addButtonAction(){
start = new JButton("Start");
stop = new JButton("Stop");
reset = new JButton("Reset");
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.startTimer();

}
});
stop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.stopTimer();

}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
tp.resetTimer();
}
});
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

这是 JPanel 类(显示时间的内容)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;

public class TimerPanel extends JPanel{
private int min,sec;
private String theTime = min + ":" + sec;
private int width=350, height=300;
private boolean timerStarted=false;
private Timer swingTimer = new Timer(900, new ActionListener(){
public void actionPerformed(ActionEvent event){
if(sec<60){
sec++;
repaint();
}else{
min++;
sec=0;
repaint();
}
}
});
//Constructor
public TimerPanel(){

setPreferredSize(new Dimension(350,300));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//start the timer
public void startTimer(){
swingTimer.start();
}

//Stop the timer
public void stopTimer(){
swingTimer.stop();
}

//reset the timer
public void resetTimer(){
sec=0; min=0;
repaint();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Paint Method
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 40));
g.drawString(theTime, width/2-45, height/2);
setBackground(Color.BLACK);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

最佳答案

请在TimerPanel.java中插入以下语句:

theTime = 分钟 + ":"+ 秒;

在每次出现repaint();之前并查看结果。

经过上述更改,TimerPanel.java 如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.*;

public class TimerPanel extends JPanel{
private int min,sec;
private String theTime = min + ":" + sec;
private int width=350, height=300;
private boolean timerStarted=false;
private Timer swingTimer = new Timer(900, new ActionListener(){
public void actionPerformed(ActionEvent event){
if(sec<60){
sec++;
theTime = min + ":" + sec;
repaint();
}else{
min++;
sec=0;
theTime = min + ":" + sec;
repaint();
}
}
});
//Constructor
public TimerPanel(){

setPreferredSize(new Dimension(350,300));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//start the timer
public void startTimer(){
swingTimer.start();
}

//Stop the timer
public void stopTimer(){
swingTimer.stop();
}

//reset the timer
public void resetTimer(){
sec=0; min=0;
theTime = min + ":" + sec;
repaint();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Paint Method
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.PLAIN, 40));
g.drawString(theTime, width/2-45, height/2);
setBackground(Color.BLACK);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

}

此外,请从 theTimer.java 中删除所有出现的 tp.updateTime();,因为不需要它。希望这可以帮助。

关于Java 计时器程序不会更新显示的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38064888/

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