读取InputStream后,JOptionPane.showMessageDialog不会显示。 【JAVA]

时间:2014-11-21 10:53:42

标签: java swing file-io joptionpane

以下是我认为导致问题的代码段。

while((is.read(bytes)) != -1)
            {   
                fos.write(bytes);
            }       

            JOptionPane.showMessageDialog(null,"File Received.","Complete.",JOptionPane.INFORMATION_MESSAGE);
            //System.out.println("File Received.");

现在当控件进入JOptionPane语句时,没有任何显示,程序甚至不会结束。我必须手动从任务管理器结束它。当我尝试注释掉JOptionPane语句并使用控制台方法,即System.out.println()来显示它工作的消息并且程序正常结束。我不明白为什么这个JOptionPane会导致这个问题。我被困在这里。帮助将不胜感激。谢谢你的期待。

1 个答案:

答案 0 :(得分:1)

完成后,您必须close。这是工作。执行fos.close()时,它会通知EDT并显示消息。在System.out.print的情况下,它不在EDT中,因此在write操作完成后打印。

while((is.read(bytes)) != -1)
            {   
                fos.write(bytes);
            }       
            fos.close();
            JOptionPane.showMessageDialog(null,"File Received.","Complete.",JOptionPane.INFORMATION_MESSAGE);
            //System.out.println("File Received.");