isGUIInitialized() 为 false,现在怎么办?

发布于 2024-12-06 11:02:24 字数 1041 浏览 0 评论 0原文

我一直在查看一些代码,发现人们在做

 public static void main(String[] args) {
     new ExampleCode();
      }
 ExampleCode () {
      EventQueue.invokeLater(this);
    }

    public void run() {
        if (EventQueueMonitor.isGUIInitialized()) {
          guiInitialized();
        } else {
          EventQueueMonitor.addGUIInitializedListener(this);
        }
  }

这很有意义,但现在我的问题是他们如何保持代码运行。据我了解,代码转到 main--->ExampleCode--->Run 然后它停止,因为 GUI 未初始化。是否有任何调用在其他地方启动 GUI?我在我的程序上使用相同的步骤,但我的 GUI 未初始化。

我的两个示例代码:

http:// /java.sun.com/javase/technologies/accessibility/docs/jaccess-1.1/examples/Explorer/Explorer.java

http://www.java2s.com/Code/Java/Swing-JFC/AGUItoshowaccessibleinformationcomingfromthecomponentsinan.htm" .htm

I been looking at some code and I find people doing

 public static void main(String[] args) {
     new ExampleCode();
      }
 ExampleCode () {
      EventQueue.invokeLater(this);
    }

    public void run() {
        if (EventQueueMonitor.isGUIInitialized()) {
          guiInitialized();
        } else {
          EventQueueMonitor.addGUIInitializedListener(this);
        }
  }

Which makes sense, but now my question is how they keep the code running. To my understanding the code goes to main--->ExampleCode--->Run and then it stops because GUI was not initialized. Does any of the calls start the GUI else where? I use the same steps on my program, but my GUI is not initialized.

Two of my example codes:

http://java.sun.com/javase/technologies/accessibility/docs/jaccess-1.1/examples/Explorer/Explorer.java

http://www.java2s.com/Code/Java/Swing-JFC/AGUItoshowaccessibleinformationcomingfromthecomponentsinan.htm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她如夕阳 2024-12-13 11:02:24

您发布的示例使用与辅助功能相关的功能,因此初始化可能需要更多时间。我们在使用 Swing 时遵循的做法是避免对事件队列进行大量初始化。原作者的逻辑是等待swing jframe等初始化完成,然后再初始化自己的组件。

// 检查 GUI 子系统是否正确初始化。 (这在 JDK 1.2 及更高版本中是必需的)。如果还没有准备好,那么我们就必须等待。 

  if (EventQueueMonitor.isGUIInitialized()) { 
    创建GUI(); 
  } 别的 { 
    EventQueueMonitor.addGUIInitializedListener(this); 
  } 
} 

公共无效guiInitialized(){ 
  创建GUI(); 
}

实际的初始化逻辑写在 createGUI 方法中,该方法要么由 Swing 调用,要么由您自己的逻辑调用。您的程序将不会终止,因为 Swing 使用它自己的非守护线程(即,除非您调用 System.exit,否则您的 swing 程序将不会终止)。

The example you posted is using accessibility related features, hence it is possible that the initialization may take more time. The practice that we follow while using Swing is to avoid heavy initialization on event queue. What the original author's logic does is that he waits for swing jframe etc. to initialize completely, then he initialized his own components.

// Check to see if the GUI subsystem is initialized correctly. (This is needed in JDK 1.2 and higher). If it isn't ready, then we have to wait. 

  if (EventQueueMonitor.isGUIInitialized()) { 
    createGUI(); 
  } else { 
    EventQueueMonitor.addGUIInitializedListener(this); 
  } 
} 

public void guiInitialized() { 
  createGUI(); 
}

The actual initialization logic is written in createGUI method, which will either be called by Swing or by your own logic. You program will not terminate, since Swing uses its own non-daemon thread (i.e. unless you call System.exit, your swing program will not terminate).

温折酒 2024-12-13 11:02:24

下面是一个基于 Swing 教程中的示例的简单示例:

import java.awt.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        add( new JLabel("Label") );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Here is a simple example based on examples from the Swing tutorial:

import java.awt.*;
import javax.swing.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        add( new JLabel("Label") );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new SSCCE() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文