Java JOptionPane对话框未打开(JOptionPane.showMessageDialog(null,“成功接收文件”)

Java JOptionPane对话框未打开(JOptionPane.showMessageDialog(null,“成功接收文件”),java,swing,Java,Swing,没有收到消息“File received Successfully”(成功接收文件),请帮助我。将文件从客户端传输到服务器时,即使收到文件,也不会显示消息 这是一个使用套接字将文件从客户机传输到服务器的程序,接口也是使用java Swigs创建的,因此请帮助我纠正错误 package Receiver; import java.util.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net

没有收到消息“File received Successfully”(成功接收文件),请帮助我。将文件从客户端传输到服务器时,即使收到文件,也不会显示消息

这是一个使用套接字将文件从客户机传输到服务器的程序,接口也是使用java Swigs创建的,因此请帮助我纠正错误

package Receiver;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Receive extends JFrame {

public Receive() {

super("Packet hiding");
//Svr3.main();
setSize(350, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);

Container c = getContentPane();
c.setLayout(new FlowLayout());



JButton readyButton = new JButton("Ready");
JButton next = new JButton("Exit");

readyButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
  try{
  ServerSocket server=new ServerSocket(127);
      Socket clientS=server.accept();
      Scanner read=new Scanner(System.in);
      Scanner scan=new Scanner(clientS.getInputStream());
      PrintWriter out=new PrintWriter(clientS.getOutputStream(),true);
      String idata,odata;
      int bytesRead;
      int current = 0;
      try
      {

        idata=scan.nextLine();
        String inp;
       inp = JOptionPane.showInputDialog(null, "Question: "+idata);

        odata=inp;
        out.println(odata);

         InputStream in = clientS.getInputStream();


    OutputStream output = new FileOutputStream("C:/Users/KRISHNASAI/Documents/NetBeansProjects/Proj1/src/Receiver/Encrypt.zip");

    byte[] buffer = new byte[1024];
    while ((bytesRead = in.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
           JOptionPane.showMessageDialog(null,"File Recieved Sucessfully");

    // Closing the FileOutputStream handle
    output.close();
    scan.close();
            out.close(); 
            clientS.close();
            server.close();

             }
            catch(Exception e)
             {
              }

             System.out.println("conversation ended");


      }
  catch(Exception e)
       {

       }    
   }
   });

next.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
  try{
  Decrypt.main(null);

  System.exit(0);



}
   catch(Exception e)
       {
         }  
     }
     });


        c.add(readyButton);

        c.add(next);  


        }

        public static void main(String args[]) throws Exception{
        Receive s = new Receive();  
        s.setVisible(true);


     }


         }

可能您可以通过“idata=scan.nextLine();”行设置一个点,单步运行将帮助您找到错误的点

OutputStream output = new FileOutputStream("your file");

byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) != -1) {
    output.write(buffer, 0, bytesRead);
}
       JOptionPane.showMessageDialog(null,"File Recieved Sucessfully");
Swing不处理事件(例如,
ActionEvent
)处理任务,包括事件调度线程(EDT)中的GUI呈现。任何类型的任务往往需要更长的时间在该线程内执行,这将阻塞该线程,因此您的应用程序将被冻结(像死了一样被卡住)

  • readyButton
    actionPerformed(ActionEvent)
    功能中删除文件传输代码

  • 创建一个可运行类
    类FileTransferHandler扩展了可运行类
    ,用必要的代码重写它的
    run()
    函数

  • readyButton
    点击:在
    actionPerformed
    函数中执行:
    new-thread(new-FileTransferHandler()).Start()启动一个线程

  • 除此之外,

     public static void main(String args[]) throws Exception{
            Receive s = new Receive();  
            s.setVisible(true);
         }
    
    正如我所说的,GUI呈现(更新)任务应该在EDT内部完成。使用
    SwingUtilities.invokeLater(Runnable)
    函数将GUI呈现任务发布到EDT

    SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                   new Receive().setVisible(true);
                }
            });
    

    可能抛出了一个异常,执行此操作时,
    catch(exception e){}
    您看不到它
    到您的捕获以查看错误。
    公共静态void main(字符串args[])引发异常{
    为什么您的main方法引发异常?您应该捕获所有异常并处理它们。