使用SwingUtilities.invokeLater制作弹出窗口

时间:2014-07-23 22:26:02

标签: java swing applet jdialog invokelater

我正在互联网上写一个回合制游戏。我尝试弹出一个应该在前面的窗口,直到输入流准备就绪。我创建了这样的smth,但似乎它不起作用。

class CustomBlockerDialog extends JDialog {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public CustomBlockerDialog(Frame owner, String text) {
    super(owner, true);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    setSize(300, 100); // Adjust if needed
    setTitle("");
    add(new JLabel(text, SwingConstants.CENTER));
} 
}




final CustomBlockerDialog block = new CustomBlockerDialog(null, "Not your turn");

SwingUtilities.invokeLater(new Runnable() {//A

    @Override
    public void run() {
        System.out.println("show");
        block.setVisible(true);
    }
});


boolean one_write_only = true;
while(in.ready()){ /* C*/ 
    if(one_write_only){
        System.out.println("waiting server");
        one_write_only = false;
    }
};

System.out.println("suppose to hide");

SwingUtilities.invokeLater(new Runnable() {//B

    @Override
    public void run() {
    System.out.println("hide");
    block.setVisible(false);
    }
});

看起来“A”和“B”在“C”之后执行,我不明白为什么。

2 个答案:

答案 0 :(得分:2)

你的问题必须是由于在Swing事件线程上而不是在后台线程中调用“C”,因为它听起来像“C”阻止事件线程运行“A”。解决方案:确保未在Swing事件线程上调用“C”。此外,如果是这种情况,并且可以通过运行SwingUtilities.isEventDispatchThread()方法进行测试,那么您不需要所有其他可运行的。

// note that this all must be called on the Swing event thread:
final CustomBlockerDialog block = new CustomBlockerDialog(null, "Not your turn");

System.out.println("show");
// block.setVisible(true);  // !! no this will freeze!

final SwingWorker<Void, Void> worker = new SwingWorker<>() {
   public void doInBackground() throws Exception {
      boolean one_write_only = true;
      while(in.ready()){ /* C*/ 
         if(one_write_only){
            System.out.println("waiting server");
            one_write_only = false;
         }
      }
   }
}

worker.addPropertyChangeListener(new PropertyChangeListener() {
   public void propertyChanged(PropertyChangeEvent pcEvt) {
      if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
         System.out.println("hide");
         block.setVisible(false);

         // call worker's get() method here and catch exceptions
      }
   }
});

worker.execute();

// moved to down here since the dialog is modal!!!
block.setVisible(true);

警告:代码未编译也未经过测试。从袖带上打字时可能会出现错误。

答案 1 :(得分:0)

感谢Hovercraft Full Of Eels,我创建了一个不同的解决方案,适用于我的情况:

final SwingWorker<Object,Object> worker2 = new SwingWorker<Object, Object>() {
        public Object doInBackground() throws Exception {
          boolean one_write_only = true;
            while(!in.ready()){ /* C*/ 
              if(one_write_only){
                System.out.println("waiting server");
                one_write_only = false;
              }
            }
            return one_write_only;
        }

        protected void done() {
            try {
                block.setVisible(false);
            } catch (Exception ignore) {}
        }

};
worker2.execute();
block.setVisible(true);