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

标签 java swing timer jpanel

我正在创建一个简单的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/

相关文章:

java - map 的 JPA map

java - 框架中面板的固定尺寸

java - JSF 2 - 找不到 Ajax 属性

c# - 计时器是否创建新线程?

java - 固定定时器和随机方向

java - Jackson 在序列化时触发 JPA Lazy Fetching

java - 使用不同名称在循环内创建对象 (JTextfields)

java - 从JTextArea中删除Text后,堆不为空

java - 从另一个 JPanel 调用 JPanel

java - Android,java定时器只工作一次