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

Java Timer Program won't update time displayed

我正在创建一个简单的 java 计时器,但负责显示时间的 JPanel 在单击 "start" 按钮时不会更新。我使用摆动计时器来更新 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 class(显示时间)

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 = min + ":" + sec;

在每次出现 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();,因为它不需要。希望这可以帮助。