自动更新jtextfield [英] automatic update jtextfield

查看:67
本文介绍了自动更新jtextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何每5秒更新一次jTextfield吗?自动的.因此,不需要用户输入.它用于更新文本字段中的时间.这是我尝试过的,但是随后程序冻结了.

Does anyone know how to update a jTextfield every 5 secondes? Automatic. So no userinput is required. It is used to update time in a textfield. This is what i tried but then my program freezes.

while(true){
                        Thread.sleep(1000);
                        txtCheck.setText(convert.getCheck());
                        System.out.println("Ja");
                        }

convert是一个线程,我尝试引发异常,但失败了,原因是Eclise说线程无法引发异常.

convert is a thread, i tried to throw an exception but failed, cause Eclise says Threads can't throw exceptions.

Convert.getCheck:

Convert.getCheck:

    public String getCheck() {
        return check;
    }

推荐答案

您要使用Swing Timer对象.这是 Oracle教程

You want to use a Swing Timer object. Here is a Oracle tutorial

首先,您需要让您的类实现ActionListener接口.在您的类内部,您还需要实现一个actionPerformed()方法.最后,您应该在main()函数或某处启动计时器

To start with, you need to have your class implement the ActionListener interface. Inside of your class, you also need to implement an actionPerformed() method. Finally, you should start the timer in your main() function or somewhere

timer = new Timer(5000, MyClass);
timer.setInitialDelay(pause);
timer.start();

然后您将像以下那样实现您的类:

You would then implement your class like:

public class MyClass implements ActionListener
{
   ...

   void actionPerformed(ActionEvent e) {
       /*
        This is called every time the timer fires, put your code here
       */
   }
 }

这篇关于自动更新jtextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆