如何每次用一个延迟的while循环更新一个jLabel [英] how to update a jLabel every time with a while loop with a delay

查看:197
本文介绍了如何每次用一个延迟的while循环更新一个jLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  int count = jSlider1.getValue(); 
  int delay = jSlider2.getValue();
    int valueOfSlider = jSlider2.getValue();
     int valueOfSlider2 = jSlider1.getValue();

while (count > 0) 
{ 
    count--;
    String count2 = ""+count; 
  jLabel3.setText(count2);
try {Thread.sleep(delay); }
catch (InterruptedException ie) { }

 }

它最终将显示jLabel上的最终数字,但它不会递增更新数字。任何帮助

It will eventually show the final number on the jLabel but it does not incrementally update the number. any help

推荐答案

Swing是单线程。因此,长时间运行的任务不应该发生在EDT 中。这包括睡觉。而应使用 javax.swing.Timer 。这将延迟后台线程,然后发布要在EDT中执行的操作。

Swing is single-threaded. Therefore, long-running tasks should never take place in the EDT. This includes sleeping. Instead, use a javax.swing.Timer. This will delay in a background thread, and then post an action to be executed in the EDT.

另请参阅:

  • How to Use Swing Timers
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public final class JLabelUpdateDemo {

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame("Update JLabel Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(JTimerLabel.getInstance());
        frame.setSize(new Dimension(275, 75)); // used for demonstration purposes
        //frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Timer t = new Timer(1000, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                int val = Integer.valueOf(JTimerLabel.getInstance().getText());
                JTimerLabel.getInstance().setText(String.valueOf(++val));
            }
        });
        t.start();
    }

    private static final class JTimerLabel extends JLabel{
        private static JTimerLabel INSTANCE;

        private JTimerLabel(){
            super(String.valueOf(0));
            setFont(new Font("Courier New", Font.BOLD,  18));
        }

        public static final JTimerLabel getInstance(){
            if(INSTANCE == null){
                INSTANCE = new JTimerLabel();
            }

            return INSTANCE;
        }
    }
}

http://www.sscce.org> SSCCE 模仿一个每秒从 0 开始计数的计数器(即更新 JLabel 实例),直到应用程序终止。

This SSCCE imitates a counter that will count up from 0 every second (i.e. update the JLabel instance) until the application is terminated.

这篇关于如何每次用一个延迟的while循环更新一个jLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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