Java定时器和文本字段

Java定时器和文本字段,java,timer,textfield,Java,Timer,Textfield,我有一个面板,其中有一个不可编辑的文本字段。我希望能够添加textField.setText(计时器)面板中的某种东西。我基本上只希望JTextArea像这样显示:0:0:0 我试过制作计时器、计时器、动作监听器等等。我搞不懂这个窍门。也许是因为我在哪里加了定时器?此时,我尝试将其添加到一个公共void guiComponents()中,抛出异常{…}。此方法保存面板、其所在的框架和其他组件的所有属性 也许我没有低估计时器的全部概念。最初,我尝试使用双嵌套for循环来完成此任务,但由于这必须在程

我有一个面板,其中有一个不可编辑的文本字段。我希望能够添加
textField.setText(计时器)面板中的某种东西。我基本上只希望JTextArea像这样显示:0:0:0

我试过制作计时器、计时器、动作监听器等等。我搞不懂这个窍门。也许是因为我在哪里加了定时器?此时,我尝试将其添加到一个
公共void guiComponents()中,抛出异常{…}
。此方法保存面板、其所在的框架和其他组件的所有属性


也许我没有低估计时器的全部概念。最初,我尝试使用双嵌套for循环来完成此任务,但由于这必须在程序其余部分运行时继续,因此我无法做到这一点。

不要为此使用JTextField。使用JLabel显示文本。比如:

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

public class TimerTime extends JFrame implements ActionListener
{
    JLabel timeLabel;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        getContentPane().add(timeLabel, BorderLayout.NORTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
    }

    public static void main(String[] args)
    {
        TimerTime frame = new TimerTime();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);

        int time = 1000;
        javax.swing.Timer timer = new javax.swing.Timer(time, frame);
        timer.setInitialDelay(1);
        timer.start();
    }
}

不要为此使用JTextField。使用JLabel显示文本。比如:

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

public class TimerTime extends JFrame implements ActionListener
{
    JLabel timeLabel;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        getContentPane().add(timeLabel, BorderLayout.NORTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
    }

    public static void main(String[] args)
    {
        TimerTime frame = new TimerTime();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);

        int time = 1000;
        javax.swing.Timer timer = new javax.swing.Timer(time, frame);
        timer.setInitialDelay(1);
        timer.start();
    }
}

我不知道日期做了什么。但我不想要实时的。更像是在应用程序开始运行后向上计数,它将继续递增直到结束。@selffally这个概念是一样的。在
actionPerformed
方法中,需要递增计数器并格式化输出。我敢肯定你至少有能力知道muchI不知道Date做了什么。但我不想要实时的。更像是在应用程序开始运行后向上计数,它将继续递增直到结束。@selffally这个概念是一样的。在
actionPerformed
方法中,需要递增计数器并格式化输出。我相信你至少能做到这一点