1

run() 方法中的代码没有被执行。谁能告诉我为什么?

startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(creator == null) {
                    String[] args = getArguments();
                    try {
                        writeSettingsFile(args);
                    } catch(IOException io) {}

                    consolePanel.addLine("Starting...");
                    bar.setIndeterminate(true);
                    try {
                        SwingUtilities.invokeAndWait(new Runnable() {

                            public void run() {
                                MessageConsole console = new MessageConsole(getConsole().getTextPane(), self, null);
                                console.redirectOut(new Color(240, 240, 240), null);
                                console.redirectErr(Color.RED, null);
                                console.setMessageLines(consolePanel.getHeight() / 17);
                                try {
                                    SomeApp.main(getArguments());
                                } catch (Exception ex) {
                                    Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        });
                    } catch (InterruptedException ex) {
                        Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InvocationTargetException ex) {
                        Logger.getLogger(OSXWorldPanel.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }
        });

引发错误:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
    at java.awt.EventQueue.invokeAndWait(EventQueue.java:1017)
    at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1320)
4

1 回答 1

3

如果您在 EDT 中,则不必调用 invokeAndWait。在您的示例中,您似乎在 EDT 中。

于 2011-01-07T20:46:48.323 回答