简体   繁体   English

与在Java Swing程序中使用SwingUtilities.invokeLater()方法有关的一些疑问

[英]Some doubts related to the use of the SwingUtilities.invokeLater() method in Java Swing program

I am studying Java Swing and I have some doubt related to this simple code tutorial that I am reading: 我正在研究Java Swing,并且与我正在阅读的这个简单的代码教程有一些疑问:

package com.andrea.execute;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/* An istance of a SimpleExample class is a JFrame object: a top level container */
public class SimpleExample extends JFrame {

    /* Create a new SimpleExample object (that is a JFrame object) and set some property */
    public SimpleExample() {
        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);                // Center the window on the screen. 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SimpleExample ex = new SimpleExample();
                ex.setVisible(true);
            }
        });
    }
}

The logic is pretty simple: I have a SimpleExample class that inherits from JFrame Swing class. 逻辑非常简单:我有一个SimpleExample类,该类继承自JFrame Swing类。 So a SimpleExample will be a toplevel container. 因此, SimpleExample将是顶级容器。

This class contain also the main() method and now I have 2 doubts: 该类还包含main()方法,现在我有2个疑问:

1) Why in the main() method is execute this code: 1)为什么在main()方法中执行以下代码:

SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                SimpleExample ex = new SimpleExample();
                ex.setVisible(true);
            }
        });

It call the invokeLater() method on the SwingUtilities class and pass to it a new Runnable oject. 它在SwingUtilities类上调用invokeLater()方法,并将新的Runnable对象传递给该方法。

Reading the documentation I know that is a way to places the application on the Swing Event Queue. 阅读文档,我知道这是将应用程序放置在Swing事件队列中的一种方法。 It is used to ensure that all UI updates are concurrency-safe. 它用于确保所有UI更新都是并发安全的。

The thing that I have some problem to understand is how it is implemented. 我要理解的问题是如何实现它。

Inside the input parameter of invokeLater() method it pass this "stuff": invokeLater()方法的输入参数内 ,它传递了这个“东西”:

new Runnable() {
        @Override
        public void run() {
            SimpleExample ex = new SimpleExample();
            ex.setVisible(true);
        }
    });

What is it this stuff? 这东西是什么? What represents? 什么代表? and how it work? 以及它是如何工作的?

Tnx 特纳克斯

Andrea 安德里亚

It is an anonymous class that implements Runnable interface . 这是一个实现Runnable接口匿名类

You can use it without anonymous class like: 您可以在没有匿名类的情况下使用它:

class SimpleExample extends JFrame {

    public SimpleExample() {
        setTitle("Simple example");
        setSize(300, 200);
        setLocationRelativeTo(null);                
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new App());
    }
}

class App implements Runnable {

    public void run() {
        SimpleExample ex = new SimpleExample();
        ex.setVisible(true);
    }
}

But anonymous class is more convenient. 但是匿名类更方便。

Runnable is an interface that contain the method run. Runnable是一个包含方法run的接口。 The invokeLater need this implementation of runnable to invoke the passed run method from the swing thread. invokeLater需要此runnable的实现才能从swing线程调用传递的run方法。

See this link to have better information http://en.wikibooks.org/wiki/Java_Programming/Threads_and_Runnables 请参阅此链接以获取更多信息http://en.wikibooks.org/wiki/Java_Programming/Threads_and_Runnables

javax.swing.SwingUtilities#invokeLater(Runnable doRun) ,调度java.lang.Runnable在事件线程上执行(可以从任何线程调用);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM