Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建计时器并在文本框中显示(通过按钮激活)_Java_Timer_Textfield - Fatal编程技术网

Java 创建计时器并在文本框中显示(通过按钮激活)

Java 创建计时器并在文本框中显示(通过按钮激活),java,timer,textfield,Java,Timer,Textfield,据我所知,它应该是这样简单的事情,但是,我没有得到任何错误,程序也没有运行 public class test extends Applet implements ActionListener { JTextField tf = new JTextField(); Timer timer = new Timer(); int counter = 30; public void init() { setLayout(null); tf.setBounds(10,10,10

据我所知,它应该是这样简单的事情,但是,我没有得到任何错误,程序也没有运行

public class test extends Applet implements ActionListener
{
  JTextField tf = new JTextField();
  Timer timer = new Timer();
  int counter = 30;

  public void init() {
  setLayout(null);
  tf.setBounds(10,10,100,40);
  tf.addActionListener(this);
  add(tf);
}

public void actionPerformed(ActionEvent e)
{
    timer.scheduleAtFixedRate(new TimerTask() {
          public void run() {
            counter = counter - 1;
          }
        }, 2*60*1000, 2*60*1000);
    tf.setText(""+counter);
}
 public static void main(String[] args) {

 }
}
请记住,我是一个完全的初学者,是的,在来这里寻求帮助之前,我在网上搜索了一段时间,请尽可能详细

非常感谢您的帮助/批评! 提前谢谢你

您有一个没有任何代码的main()。这是应用程序应该初始化的地方:

public static void main(String[] args) {
    test app = new test(); 
    app.init();
 }

否则,
测试
课程将永远不会打开。

谢谢。可视化是固定的,但是功能没有运行。(即倒计时计时器)计时器的逻辑没有真正意义。您正在创建一个JTextField,并通过actionListener将计时器添加到此字段中。我认为这个计时器也应该在init()方法中初始化。虽然我对计时器不是很熟悉。