使用swing在java中计算和打印时间

使用swing在java中计算和打印时间,java,multithreading,swing,Java,Multithreading,Swing,我正在尝试使用一个线程实现一个计时器,并使用另一个线程在JButton上打印它。 我的时间课是这样的: public class Time extends Thread { int counter = 0; public String currentTime = new String(); public String printFormat(int second) { return String.format("%d:%d", second/60, second%60); } s

我正在尝试使用一个线程实现一个计时器,并使用另一个线程在JButton上打印它。 我的时间课是这样的:

public class Time extends Thread 
{
int counter = 0;
public String currentTime = new String();


public String printFormat(int second)
{
    return String.format("%d:%d", second/60, second%60);
}

synchronized public void count(int minute) throws InterruptedException
{
    minute *= 60;
    while(minute >= 0)
    {
        wait(1000);
        minute--;
        currentTime = printFormat(minute);
        System.out.println(currentTime);
    }

}
     button.setText(time.currentTime);
我的主线是这样的:

public class Time extends Thread 
{
int counter = 0;
public String currentTime = new String();


public String printFormat(int second)
{
    return String.format("%d:%d", second/60, second%60);
}

synchronized public void count(int minute) throws InterruptedException
{
    minute *= 60;
    while(minute >= 0)
    {
        wait(1000);
        minute--;
        currentTime = printFormat(minute);
        System.out.println(currentTime);
    }

}
     button.setText(time.currentTime);
这段代码有什么问题

“如果您能用java swing定时器解释一下,我将不胜感激”

如果您想使用
javax.swing.Timer
执行以下操作,非常简单

  • 与将
    ActionListener
    设置为按钮的方式相同,您也可以对计时器执行相同的操作。除了触发事件的按钮之外,它是由计时器触发的,您为它设置的每个持续时间段都会触发
  • 如果是像时钟一样的计时器,你可以将其设置为1000, 每1000毫秒做一次
在这方面 例如,我只是将按钮的文本设置为 每次触发计时器事件时递增一。这是
计时器
代码

Timer timer = new Timer(1000, new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        button.setText(String.valueOf(count));
        count++;
    }
});
timer.start();
正如你所看到的,这很简单

您可以运行这个示例

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class ButtonTimer {

    private JButton button = new JButton(" ");
    private int count = 1;
    public ButtonTimer() {

        Timer timer = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                button.setText(String.valueOf(count));
                count++;
            }
        });
        timer.start();

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

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

如果你想帮助找出你当前的代码,考虑发布一个可运行的程序,我们可以测试。这样我们就能看出你错在哪里了



这是一个关于这段代码有什么问题的教程?它支持做什么?它应该打印一个文本,它是计时器,在按钮上,主要是,我想在我的游戏中添加一个计时器。这是一个要求,使用两个线程吗?使用
javax.swing.Timer可以很容易地实现这一点,我们的任务是使用这种方法,但是如果您可以使用javaswing-Timer解释它,我将非常感激;)。这段代码有什么问题?当你运行它时会发生什么?thanx bro。坦克斯兄弟:没问题,没问题。如果需求是为了学习线程而使用两个线程,我建议您学习它们。