如何在EDT上运行JOptionPane? [英] How do I run JOptionPane on EDT?

查看:132
本文介绍了如何在EDT上运行JOptionPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然有空白 JOptionPane 的问题。基于SO和 Java Docs 的研究,这显然与不使用 EDT 有关。我的问题是 EDT 及其方法究竟是如何适用于 JOptionPane ?例如,终端错误输出很明显,下面的 JOptionPane 不会在 EDT 上运行。具体缺少什么,以及 Runnable 之类的内容如何适用?

  import javax.swing。*; 
公共类PaneDemo
{
public static void main(String [] args)
{
final String [] TEXT = {
// message
Hello,World!,
// title
Greeting}; //结束TEXT
showMyPane(TEXT);
} // end main
public static void showMyPane(final String [] TEXT)
{
JOptionPane.showMessageDialog(null,TEXT [0],TEXT [1],
JOptionPane.INFORMATION_MESSAGE);
if(!SwingUtilities.isEventDispatchThread())
{
System.err.println(Err:GUI未能使用EDT。);
} //结束if(!SwingUtilities.isEventDispatchThread())
} // end showMyPane
} //结束类PaneDemo

答案建议添加 invokeLater 。但是,在BlueJ中似乎并没有很好地呈现。





此外,isEventDispatchThread()仍在终端中返回错误。是因为它现在位于错误的位置吗?

解决方案

您可以创建 JOptionPane 在它应该可以帮助你理解它的全部内容



更新:



根据评论,您应该在<$上启动 JOptionPane c $ c> EDT 每次拨打 showPane(...)方法如下:

  public static void showMyPane(final String [] TEXT){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
JOptionPane.showMessageDialog(null,TEXT [0]
+\ n在EDT上:+ SwingUtilities.isEventDispatchThread(),TEXT [1],
JOptionPane.INFORMATION_MESSAGE);
}
});
} //结束showMyPane

public static void main(String [] args){
final String [] TEXT = {
// message
Hello,World!,
// title
Greeting}; //结束TEXT
showMyPane(TEXT);
}


I'm still having problems with blank JOptionPanes. Based on research at SO and in Java Docs, this obviously has something to do with not using the EDT. My question is how exactly do the EDT and its methods apply to JOptionPane? For example, the terminal error output makes it quite clear that the JOptionPane below is not run on the EDT. What's missing specifically, and how does something like Runnable fit in?

import javax.swing.*;
public class PaneDemo
{
public static void main(String[] args)
{
    final String[] TEXT ={
            //message
            "Hello, World!",
            //title
            "Greeting"};//end TEXT
    showMyPane(TEXT);
}//end main
public static void showMyPane(final String[] TEXT)
{
    JOptionPane.showMessageDialog(null, TEXT[0], TEXT[1], 
        JOptionPane.INFORMATION_MESSAGE);
    if(!SwingUtilities.isEventDispatchThread())
    {
        System.err.println("Err: GUI failed to use EDT.");
    }//end if(!SwingUtilities.isEventDispatchThread())
}//end showMyPane
}//end class PaneDemo

An answer suggested adding invokeLater. That doesn't seem to render very well in BlueJ, however.

Also isEventDispatchThread() is still returning the error in the terminal. Is that simply because it is now in the wrong location?

解决方案

You can create JOptionPane on the Event Dispatch Thread like this:

  final String[] TEXT = {
        //message
        "Hello, World!",
        //title
        "Greeting"};//end TEXT

     ...

    /**
     * Create GUI and components on Event-Dispatch-Thread
     */
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
                JOptionPane.showMessageDialog(null, TEXT[0] 
                      + "\n is on EDT: " + SwingUtilities.isEventDispatchThread(), TEXT[1],
                        JOptionPane.INFORMATION_MESSAGE);
        }
    });

Have a look at the Lesson: Concurrency in Swing it should help you understand what its all about

UPDATE:

as per comment you should initiate the JOptionPane on the EDT on each call in showPane(...) method like so:

   public static void showMyPane(final String[] TEXT) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, TEXT[0] 
                      + "\n is on EDT: " + SwingUtilities.isEventDispatchThread(), TEXT[1],
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });
    }//end showMyPane

    public static void main(String[] args) {
        final String[] TEXT = {
            //message
            "Hello, World!",
            //title
            "Greeting"};//end TEXT
        showMyPane(TEXT);
    }

这篇关于如何在EDT上运行JOptionPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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