JOptionPane对话框未打开(JOptionPane.showMessageDialog(null,“File Received Successfully”))

时间:2013-11-12 13:58:52

标签: java swing

没有收到消息“文件已成功接收”请帮助我。虽然文件已收到,但是将文件从客户端传输到服务器时,它不显示消息。

这是使用套接字将文件从客户端传输到服务器的程序,并且也使用java swings创建接口,所以请帮助我纠正错误

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);


     }


         }

2 个答案:

答案 0 :(得分:1)

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呈现。任何类型的任务往往需要花费更长的时间在此线程内执行将阻止此线程,因此您的应用程序将被冻结(坚持就像死了)。

  1. readyButton actionPerformed(ActionEvent)功能中删除传输代码的文件。

  2. 创建一个Runnable类class FileTransferHandler extends Runnable,用必要的代码覆盖它的run()函数。

  3. 使用readyButton上的runnable类启动一个主题:在actionPerformed函数内执行:new Thread(new FileTransferHandler()).start()

  4. 除了这些变化,

     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);
                }
            });
    

答案 1 :(得分:0)

可能是你可以逐行设置“idata = scan.nextLine();”,步骤运行会帮你找错