gpt4 book ai didi

java - JOptionPane 对话框未打开(JOptionPane.showMessageDialog(null ,"File Recieved Sucessfully"))

转载 作者:行者123 更新时间:2023-12-01 13:51:22 28 4
gpt4 key购买 nike

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

这是使用套接字将文件从客户端传输到服务器的程序,接口(interface)也是使用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);


}


}

最佳答案

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 在事件调度线程 (EDT) 中执行事件(例如 ActionEvent )处理任务,包括 GUI 渲染。任何类型的任务往往需要更长的时间在该线程内执行,都会阻塞该线程,因此您的应用程序将被卡住(卡住就像死了一样)。

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

  2. 创建一个 Runnable 类 class FileTransferHandler extends Runnable ,覆盖它的 run()具有必要代码的功能。

  3. 使用 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);
}
});

关于java - JOptionPane 对话框未打开(JOptionPane.showMessageDialog(null ,"File Recieved Sucessfully")),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19931142/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com