JFrame上的计数器 [英] Counter on JFrame

查看:119
本文介绍了JFrame上的计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建JFrame并查看计数器的进度.

I am trying to make a JFrame and see the progress of the counter.

int i = 1;
while (i < 100000){
    textField.setText(String.valueOf(i));
    System.out.println(i);
    i++;
}

启动时,我可以在控制台上看到进度,但是textField的值没有改变.循环结束时变为100000.

When I start it I can see the progress at the console but the value of the textField does not change. It changes to 100000 when the loop ends.

如何使其像控制台一样显示进度?

How can I make it show the progress like in console?

推荐答案

与其他GUI工具包(例如C#)之间有许多重要区别.

There are a number of important differences between and other GUI toolkits like C#.

首先,Swing组件共享一个公共的本地对等体.在许多其他GUI框架中,组件具有自己的本机对等体,这会影响您可以访问这些组件的上下文.

Firstly, Swing components SHARE a common native peer. In many other GUI frameworks, components have their own native peer, this affects the context in which how you can access these components.

其次,由于Swing组件共享一个公共的本机对等体,因此具有内在的线程安全性(例如,它们都共享相同的消息队列),这意味着您永远不要在Event上下文之外修改UI组件.调度线程.

Secondly, because Swing components share a common native peer, there are inherently un-thread safe (they all share the same message queue for example), this means you should never modify a UI component out side of the context of the Event Dispatching Thread.

第三,永远不要阻塞事件调度线程",这将阻止它处理新事件,包括绘画请求.

Thirdly, you should never block the Event Dispatching Thread, this will prevent it from process new events, including paint requests.

在这种情况下,您应该应该使用sa javax.swing.Timer,这将允许您定期安排回调(将在EDT的上下文中发生),从而可以安全地在例如Swing框架...

In this context, you should should probably use s a javax.swing.Timer, which will allow you to schedule a callback (which will occur within the context of the EDT) at a regular interval, making it safe to use within the context of the Swing framework, for example...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Counter {

    public static void main(String[] args) {
        new Counter();
    }

    public Counter() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private Timer timer;
        private int count;

        public TestPane() {
            label = new JLabel("...");
            setLayout(new GridBagLayout());
            add(label);
            timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    if (count < 100000) {
                        label.setText(Integer.toString(count));
                    } else {
                        ((Timer)(e.getSource())).stop();
                    }
                }
            });
            timer.setInitialDelay(0);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }

}

看看 Swing中的并发

Take a look at Concurreny in Swing and How to Use Swing Timers for more details...

这篇关于JFrame上的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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