Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有人能解释一下javax.swing.SwingUtilities.invokeLater在这段代码中的用法吗_Java_Swing_Interface - Fatal编程技术网

有人能解释一下javax.swing.SwingUtilities.invokeLater在这段代码中的用法吗

有人能解释一下javax.swing.SwingUtilities.invokeLater在这段代码中的用法吗,java,swing,interface,Java,Swing,Interface,我发现这个代码很奇怪。因为在圆括号关闭之前还有另一个函数。你能解释一下它是怎么工作的吗 javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); ------完整代码如下。这个密码不属于我。我在谷歌搜索时找到的 package pkginterface; import java.awt.FlowLayou

我发现这个代码很奇怪。因为在圆括号关闭之前还有另一个函数。你能解释一下它是怎么工作的吗

javax.swing.SwingUtilities.invokeLater(new Runnable() {     
    public void run() {
        createAndShowGUI(); 
    }
});
------完整代码如下。这个密码不属于我。我在谷歌搜索时找到的

package pkginterface;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Interface extends JFrame implements ActionListener {    

    private static final long serialVersionUID = 1L;

    public Interface() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());
        JButton button = new JButton("Java Code Geeks - Java Examples");
        //add Button
        add(button);
        //set action listeners for buttons
        button.addActionListener(this);
        // define a custom short action command for the button
        button.setActionCommand("Geeks");
        // add button to frame
        add(button);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String action = ae.getActionCommand();
        if (action.equals("Geeks")) {
            System.out.println("Button pressed!");
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new Interface();
        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(); 
            }
        });
    }
}

它将事件发布到Swing的事件调度线程。
该事件导致在EDT线程上调用createAndShowUi()方法。

我想您是在询问如何创建EventDispatchThread以及如何正确使用初始线程(Oracle教程是Swing中Concurrency的一部分)