通过计时器在JDialog中设置动态JLabel文本 [英] Set dynamic JLabel text in a JDialog by timer

查看:136
本文介绍了通过计时器在JDialog中设置动态JLabel文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个JDialog,该对话框将向用户显示JLabel上的动态消息. 该消息应该是从1到10的计数(并且应该每秒更改一个数字). 问题是,当我进行调试时-它在"dia.setVisible(true);"之后立即停止,除非我将关闭JDialog,否则它将不会继续. 有什么可能的解决办法? 谢谢.

Im trying to make a JDialog that will show the user a dynamic message on a JLabel. The message should be a count from 1 to 10 (and it should be changing a number every second). the thing is , when im debugging it - it's stops right after the "dia.setVisible(true);" , and will not proceed unless i will close the JDialog . is there any possible way of fixing it? Thanks.

看看代码:

    @Override
public void run() {

    dia = new JDialog(parentDialog, true);
    dia.setLocationRelativeTo(parentFrame);


    String text = "Text ";
    dia.setSize(300, 150);
    jl = new JLabel(text);
    dia.getContentPane().add(jl);
    dia.setVisible(true);
    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(1000);
            jl.setText(text + " " + i);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

推荐答案

  • 不要在Swing GUI中使用Thread.sleep(int),导致冻结直到Thread.sleep(int)结束

    • don't use Thread.sleep(int) for Swing GUI, caused freeze of untill Thread.sleep(int) ended

      使用Swing Timer代替通过使用Thread.sleep(int)

      不使用dia.setSize(300, 150),了解LayoutManager的工作方式

      don't use dia.setSize(300, 150), learn how LayoutManager works

      这篇关于通过计时器在JDialog中设置动态JLabel文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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