Java:线程化时更改标签值 [英] Java: Changing a label value while threading

查看:72
本文介绍了Java:线程化时更改标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小循环,在按下GUI上的一个按钮后,该值将每秒更改一次;我尝试使用线程,但似乎无法正常工作.现在发生的是,程序在按下按钮后仅暂停了10秒钟.你能帮我吗?

i want to create a little loop where after i press a button on a GUI, a value will change every second; i have tried using a thread but i can't seem to get it working correctly. What now happens is that the program just pauses for 10 seconds after pressing the button. Could you help me?

这是我的代码的样子

private void ButtonActionPerformed(java.awt.event.ActionEvent evt) { 

for (x = 0; x <= 10; x++)
    {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt(); here.
        }


        nummerlabel.setText(String.valueOf(x));
    }



}

推荐答案

我认为您正在寻找的是swing包随附的Timer类.阅读文档,它应该可以帮助您解决问题问题.

I think what you're looking for is the Timer class which comes with the swing package. Have a read of the documentation, it should help you solve your problem.

这篇关于Java:线程化时更改标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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