怎么判断一个界面是否已关闭。 自己定义一个boolean变量标识窗口是否关闭,然后添加windowListener监听windowClosing事件,在实现的方法里边进行状态修改。根据这个变量就可以判断了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道采用map是否可以解决。我的思路:打开对话界面时,双方将对方身份唯一标识存入map。关闭对话界面,从map将该标识移除。双方在接受对方发来的信息时,首先从map中取值,如果能够取到值,表示界面已存在,不新建;如果未取到值,表示自己已经关闭了对话界面,新建界面。 关掉界面,那么连接也就断开了。 socket通讯如果一方连接断开,则会抛出一个IO异常(IOException),你可以在这个IO异常里处理你的逻辑 如果楼主熟悉异常机制的话,在异常中处理是一个不错的方法,也可以使用楼上讲的map来实现,socket都有一个唯一的标识,这种方式比较方便的,我跟楼主分享个设计,可以不用总是关闭重新创建窗口,如果用的是swing编程,本来swing画界面很慢的,楼主可以设计一个缓存池,缓存池中存放的是界面,当然每一个界面绑定一个唯一的标识和用户状态和使用频率,这个可以用java的封装性来实现,然后重写必要的方法,如hashCode和equals方法,这样就可以节省一些性能。希望楼主学习进步... 我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。 我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。怎么可能不断开!要么就是在等连接超时。反正是会断开的 我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。会调用windowClosing的,我用下面例子做过测试。public class LoginFrame extends JFrame { public LoginFrame() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("WindowClosing..."); } @Override public void windowClosed(WindowEvent e) { System.out.println("WindowClosed..."); } }); setVisible(true); } public static void main(String[] args) { new LoginFrame(); }}点击右上角红叉关闭时,是会调用windowClosing和windowClosed的 我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。怎么可能不断开!要么就是在等连接超时。反正是会断开的我不都说了嘛。。连接跟对话是不同的界面。有图为证:关闭对话界面前:关闭对话界面后:我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。会调用windowClosing的,我用下面例子做过测试。public class LoginFrame extends JFrame { public LoginFrame() { setDefaultCloseOperation(DISPOSE_ON_CLOSE); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println("WindowClosing..."); } @Override public void windowClosed(WindowEvent e) { System.out.println("WindowClosed..."); } }); setVisible(true); } public static void main(String[] args) { new LoginFrame(); }}点击右上角红叉关闭时,是会调用windowClosing和windowClosed的 public void windowClosing(WindowEvent e) { System.out.println("*********closing**************"); } public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("*********closed**************"); }我也写了哦。结果见上图。。没有触发。 windowClosing是会触发的,楼主把UI的完整代码贴出来看看 有点长。而且我没写注释。import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.Socket;import javax.swing.*;public class chat extends JFrame implements ActionListener,WindowListener{ String tit,opip,myname,size; String[] units = new String[]{"B","KB","MB","GB","TB","PB"}; double mod = 1024.0; int opport,sendname=-1; JButton b1,b2,b3,b4,b5; JTextArea a1,a2,a3,a4; JTextField t1; JFileChooser fc; PrintWriter out; Socket myClient; JLabel j5; DataOutputStream out2; DataInputStream in2; indexset xx; chatinter cc; //chat rebu; int cw=0; //BufferedReader in; /*static public String geta2() { return a2.getText(); }*/ /*static public void seta1(String a) { //a1.append("我:"+msg+"\n"); //a2.setText(""); a1.append("我:"+a+"\n"); a2.setText(""); }*/ public chat(String a,int b,String c,String d,indexset xx,chatinter cc){ opip=a; opport=b; tit=c; myname=d; sendname=0; this.cc=cc; cw=1;//客户端界面关闭 this.xx=xx; setTitle("与"+tit+"聊天"); ini(); sendmsg(); } public chat(String a,Socket c,indexset xx) { tit=a; sendname=1; this.myClient=c; this.xx=xx; cw=2;//服务器界面关闭 setTitle("与"+tit+"聊天"); ini(); smsg(); } public chat() { ini(); } void smsg() { try { //while(true) //{ BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream())); out=new PrintWriter(myClient.getOutputStream()); while(true) { String talk=in.readLine(); a1.setCaretPosition(a1.getText().length()); if(talk.equals("agreetoreceivefileaaaaaaaaaa")) { String filename; long filesize; filename=in.readLine(); filesize=Long.parseLong(in.readLine()); int i; for (i = 0; filesize >= mod; i++) { filesize /= mod; } size=Math.round(filesize*10)/10.0 + units[i]; b4.setVisible(true); b5.setVisible(true); a4.append(tit+"希望向您发送文件:"+"\n"+filename+",大小:"+size+","+"\n"+"是否接收?"); //a4.append(tit+"希望向您发送文件:"+"\n"+",大小:"+size+","+"\n"+"是否接收?"); } else if(talk.equals("afsdghfdhfadhgsafg")) a1.append("对方拒绝接受文件"+"\n"); else if(talk.equals("qwetqeryfgnhdwfgyqwet")) new sendfile(fc,myClient).start(); else if(!(talk.equals(""))) a1.append(tit+":"+talk+"\n"); a1.setCaretPosition(a1.getText().length()); } } catch (Exception e1) { System.out.println(e1); } } void ini(){ //setTitle("与"+tit+"聊天"); setVisible(true); setDefaultCloseOperation(DISPOSE_ON_CLOSE); //setDefaultCloseOperation(EXIT_ON_CLOSE); setBounds(50, 50, 650, 400); setLayout(null); JLabel j1=new JLabel("聊天记录:"); add(j1); j1.setBounds(10,10,100,20); a1=new JTextArea(); add(a1); a1.setBounds(10,40,350,200); a1.setEditable(false); a1.setLineWrap(true); JScrollPane jS1 = new JScrollPane(a1); add(jS1); jS1.setBounds(10,40,350,200); //a1.append("\n"); //a1.setCaretPosition(a1.getText().length()); JLabel j2=new JLabel("输入:"); add(j2); j2.setBounds(10, 270, 50, 20); a2=new JTextArea(); add(a2); a2.setBounds(60,270,200,50); a2.setLineWrap(true); JScrollPane jS2 = new JScrollPane(a2); add(jS2); jS2.setBounds(60,270,200,50); b1=new JButton("发消息"); add(b1); b1.setBounds(300,310,80,20); b1.addActionListener(this); JLabel j3=new JLabel("文件传输"); add(j3); j3.setBounds(450, 40, 80, 20); a3=new JTextArea(); add(a3); a3.setBounds(400,60,200,100); a3.setEditable(false); JScrollPane jS3 = new JScrollPane(a3); add(jS3); jS3.setBounds(400,60,200,100); JLabel j4=new JLabel("要发送的文件:"); add(j4); j4.setBounds(400, 180, 100, 20); t1=new JTextField(); add(t1); t1.setBounds(400, 200, 200, 20); //t1.setEditable(false); b2=new JButton("选择文件"); add(b2); b2.addActionListener(this); b2.setBounds(400,230,100,20); b3=new JButton("发送文件"); add(b3); b3.addActionListener(this); b3.setBounds(510,230,100,20); a4=new JTextArea(); add(a4); a4.setBounds(430,260,200,55); a4.setEditable(false); a4.setBackground(null); //a4.setVisible(false); //add(j5); //j5.setBounds(430,260,200,100); b4=new JButton("是"); add(b4); b4.addActionListener(this); b4.setBounds(430,320,60,20); b5=new JButton("否"); add(b5); b5.addActionListener(this); b5.setBounds(520,320,60,20); b4.setVisible(false); b5.setVisible(false); /*fc=new JFileChooser(); add(fc); fc.setBounds(400, 200, 200, 200);*/ }/* try { Socket client_socket=new Socket(opip,opport); out=new PrintWriter(client_socket.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(client_socket.getInputStream())); //System.out.println(in.readLine()); //if(!in.readLine().equals("")) a1.append(tit+":"+in.readLine()+"\n"); //BufferedReader wt=new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e1) { System.out.println(e1); }*/ //System.out.println(in.readLine()); void sendmsg() { try { Socket myClient=new Socket(opip,opport); out=new PrintWriter(myClient.getOutputStream()); BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream())); //System.out.println(in.readLine()); //if(!in.readLine().equals("")) while(true) { //if(xx.win==false) // new chatinter(opip,opport,tit,myname,xx).start(); String talk=in.readLine(); System.out.println("收到消息:"+talk); a1.setCaretPosition(a1.getText().length()); if(talk.equals("agreetoreceivefileaaaaaaaaaa")) { String filename; long filesize; filename=in.readLine(); filesize=Long.parseLong(in.readLine()); int i; for (i = 0; filesize >= mod; i++) { filesize /= mod; } size=Math.round(filesize*10)/10.0 + units[i]; b4.setVisible(true); b5.setVisible(true); a4.append(tit+"希望向您发送文件:"+"\n"+filename+",大小:"+size+","+"\n"+"是否接收?"); //a4.append(tit+"希望向您发送文件:"+"\n"+",大小:"+size+","+"\n"+"是否接收?"); } else if(talk.equals("afsdghfdhfadhgsafg")) a1.append("对方拒绝接受文件"+"\n"); else if(talk.equals("qwetqeryfgnhdwfgyqwet")) new sendfile(fc,myClient).start(); else if(!(talk.equals(""))) a1.append(tit+":"+talk+"\n"); a1.setCaretPosition(a1.getText().length()); } //BufferedReader wt=new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e1) { System.out.println(e1); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { try { String msg=this.a2.getText(); a1.setCaretPosition(a1.getText().length()); if(!msg.equals("")) a1.append("我:"+msg+"\n"); a1.setCaretPosition(a1.getText().length()); //PrintWriter out=new PrintWriter(client_socket.getOutputStream()); //BufferedReader in=new BufferedReader(new InputStreamReader(client_socket.getInputStream())); //BufferedReader wt=new BufferedReader(new InputStreamReader(System.in)); //String str=a2.getText(); a2.setText(""); if(sendname==0) { out.println(myname); out.flush(); sendname++; } out.println(msg); out.flush(); } catch (Exception e1) { System.out.println(e1); } } else if(e.getSource()==b2) { fc = new JFileChooser(); int rs =fc.showOpenDialog(this); if (JFileChooser.APPROVE_OPTION == rs) { //JOptionPane.showMessageDialog(this, "确定!"); t1.setText(fc.getSelectedFile().getAbsolutePath()); //System.out.println(fc.getSelectedFile().getName()); //System.out.println(fc.getSelectedFile().length()); //double bytesize=fc.getSelectedFile().length(); //System.out.println(size); } } else if(e.getSource()==b3) { try { out2 = new DataOutputStream(new BufferedOutputStream(myClient.getOutputStream())); in2 = new DataInputStream(new BufferedInputStream(myClient.getInputStream())); out2.writeBytes("agreetoreceivefileaaaaaaaaaa"+"\n"); out2.flush(); out2.write((fc.getSelectedFile().getName()+"\n").getBytes()); out2.flush(); out2.writeBytes(fc.getSelectedFile().length()+"\n"); out2.flush(); } catch(IOException e1){e1.printStackTrace();} finally { /*try { out2.close(); in2.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }*/ } } else if(e.getSource()==b4) { a1.append("开始接受文件\n"); out.println("qwetqeryfgnhdwfgyqwet"); out.flush(); a4.setText(""); b4.setVisible(false); b5.setVisible(false); new receivefile(fc,myClient,cc).start(); } else if(e.getSource()==b5) { a1.append("拒绝接受文件\n"); out.println("afsdghfdhfadhgsafg"); out.flush(); a4.setText(""); b4.setVisible(false); b5.setVisible(false); } } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { /*if(cw==1) xx.win=false; if(cw==2) xx.winopen=false;*/ System.out.println("*********closing**************"); } public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub System.out.println("*********closed**************"); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub }} 不知道为什么中间一大串都被注释掉了。。全部复制到myeclipse里是没事的 Hi,根据楼主的描述,楼主需要的是界面关闭,但是socket不断开。在socket接受消息中保存界面的引用,当收到消息,检查window.isopen(),就可以了。思路大致如此,困难是什么? 楼主,你虽然实现了WindowListener,实现了windowClosed和windowClosing的监听方法,但是你没有将它们注册到监听器。就相当于观察者模式里边,你只将观察者实现了,没有将它注册到被观察者的监听集合里边。 意思就是说,你少了一行addWindowListener(),所以虽然触发了windowClosed和windowClosing事件,但是没有找到处理的人,你看起来就像没有触发一样。 通常在事件处理代码比较少的时候,你可以使用匿名内部类的方式去实现WindowListener接口(就像我代码里边只是输出了一个字符串)。 而如果处理代码比较多,你就可以单独建一个类来实现WindowListener接口,然后在JFrame构造方法里边加一句addWindowListener(new MyListener());这样,在事件触发的时候,就会到这个Listener里边找处理方法。然后你最开始的问题就好解决了。 不管是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)还是setDefaultCloseOperatio(DO_NOTHING_ON_CLOSE)或者其他的,都不影响这些事件的发生。 加了addWindowListener()就可以了。谢了 设计模式是好的,但是为设计模式而设计模式就有点买椟还珠的味道了 各位大侠,快来帮助我啊~ 求chm格式的中文servlet api?能否发一个 请大家帮我看看呀 java 子类一定继承父类所有的方法和属性吗 各位前辈帮帮忙! 赴港JAVA开发工程师的面试题目 请问如何在小程序(Applet)里面实现菜单(Menu)效果? JAVA程序员,月薪大调查~~~~跟贴有分!!!! 如何把ascii值转化成字符,字符串中如何包含一个双引号? 请问,在计算散列码时这些数字是固定的吗? j2SE B/S结构 数据库的并发新手问题
我的思路:打开对话界面时,双方将对方身份唯一标识存入map。关闭对话界面,从map将该标识移除。双方在接受对方发来的信息时,首先从map中取值,如果能够取到值,表示界面已存在,不新建;如果未取到值,表示自己已经关闭了对话界面,新建界面。
希望楼主学习进步...
我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。
我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。
怎么可能不断开!要么就是在等连接超时。反正是会断开的
我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。
会调用windowClosing的,我用下面例子做过测试。
public class LoginFrame extends JFrame { public LoginFrame() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowClosing...");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("WindowClosed...");
}
});
setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
}
点击右上角红叉关闭时,是会调用windowClosing和windowClosed的
我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。
怎么可能不断开!要么就是在等连接超时。反正是会断开的我不都说了嘛。。连接跟对话是不同的界面。有图为证:
关闭对话界面前:关闭对话界面后:我的窗口关闭用的是setDefaultCloseOperatio(DISPOSE_ON_CLOSE)。不触发windowClosing。另外我的界面是分成连接界面和对话界面两部分的。对话界面关闭以后socket没断开还在的。
会调用windowClosing的,我用下面例子做过测试。
public class LoginFrame extends JFrame { public LoginFrame() {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("WindowClosing...");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("WindowClosed...");
}
});
setVisible(true);
}
public static void main(String[] args) {
new LoginFrame();
}
}
点击右上角红叉关闭时,是会调用windowClosing和windowClosed的
public void windowClosing(WindowEvent e)
{
System.out.println("*********closing**************");
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("*********closed**************");
}我也写了哦。结果见上图。。没有触发。
有点长。而且我没写注释。import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;import javax.swing.*;public class chat extends JFrame implements ActionListener,WindowListener{
String tit,opip,myname,size;
String[] units = new String[]{"B","KB","MB","GB","TB","PB"};
double mod = 1024.0;
int opport,sendname=-1;
JButton b1,b2,b3,b4,b5;
JTextArea a1,a2,a3,a4;
JTextField t1;
JFileChooser fc;
PrintWriter out;
Socket myClient;
JLabel j5;
DataOutputStream out2;
DataInputStream in2;
indexset xx;
chatinter cc;
//chat rebu;
int cw=0;
//BufferedReader in;
/*static public String geta2()
{
return a2.getText();
}*/
/*static public void seta1(String a)
{
//a1.append("我:"+msg+"\n");
//a2.setText("");
a1.append("我:"+a+"\n");
a2.setText("");
}*/
public chat(String a,int b,String c,String d,indexset xx,chatinter cc){
opip=a;
opport=b;
tit=c;
myname=d;
sendname=0;
this.cc=cc;
cw=1;//客户端界面关闭
this.xx=xx;
setTitle("与"+tit+"聊天");
ini();
sendmsg();
}
public chat(String a,Socket c,indexset xx)
{
tit=a;
sendname=1;
this.myClient=c;
this.xx=xx;
cw=2;//服务器界面关闭
setTitle("与"+tit+"聊天");
ini();
smsg();
}
public chat()
{
ini();
}
void smsg()
{
try
{
//while(true)
//{
BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream()));
out=new PrintWriter(myClient.getOutputStream());
while(true)
{
String talk=in.readLine();
a1.setCaretPosition(a1.getText().length());
if(talk.equals("agreetoreceivefileaaaaaaaaaa"))
{
String filename;
long filesize;
filename=in.readLine();
filesize=Long.parseLong(in.readLine());
int i;
for (i = 0; filesize >= mod; i++) {
filesize /= mod;
}
size=Math.round(filesize*10)/10.0 + units[i];
b4.setVisible(true);
b5.setVisible(true);
a4.append(tit+"希望向您发送文件:"+"\n"+filename+",大小:"+size+","+"\n"+"是否接收?");
//a4.append(tit+"希望向您发送文件:"+"\n"+",大小:"+size+","+"\n"+"是否接收?");
}
else if(talk.equals("afsdghfdhfadhgsafg"))
a1.append("对方拒绝接受文件"+"\n");
else if(talk.equals("qwetqeryfgnhdwfgyqwet"))
new sendfile(fc,myClient).start();
else if(!(talk.equals("")))
a1.append(tit+":"+talk+"\n");
a1.setCaretPosition(a1.getText().length());
}
}
catch (Exception e1)
{
System.out.println(e1);
}
}
void ini(){
//setTitle("与"+tit+"聊天");
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(50, 50, 650, 400);
setLayout(null);
JLabel j1=new JLabel("聊天记录:");
add(j1);
j1.setBounds(10,10,100,20);
a1=new JTextArea();
add(a1);
a1.setBounds(10,40,350,200);
a1.setEditable(false);
a1.setLineWrap(true);
JScrollPane jS1 = new JScrollPane(a1);
add(jS1);
jS1.setBounds(10,40,350,200);
//a1.append("\n");
//a1.setCaretPosition(a1.getText().length());
JLabel j2=new JLabel("输入:");
add(j2);
j2.setBounds(10, 270, 50, 20);
a2=new JTextArea();
add(a2);
a2.setBounds(60,270,200,50);
a2.setLineWrap(true);
JScrollPane jS2 = new JScrollPane(a2);
add(jS2);
jS2.setBounds(60,270,200,50);
b1=new JButton("发消息");
add(b1);
b1.setBounds(300,310,80,20);
b1.addActionListener(this);
JLabel j3=new JLabel("文件传输");
add(j3);
j3.setBounds(450, 40, 80, 20);
a3=new JTextArea();
add(a3);
a3.setBounds(400,60,200,100);
a3.setEditable(false);
JScrollPane jS3 = new JScrollPane(a3);
add(jS3);
jS3.setBounds(400,60,200,100);
JLabel j4=new JLabel("要发送的文件:");
add(j4);
j4.setBounds(400, 180, 100, 20);
t1=new JTextField();
add(t1);
t1.setBounds(400, 200, 200, 20);
//t1.setEditable(false);
b2=new JButton("选择文件");
add(b2);
b2.addActionListener(this);
b2.setBounds(400,230,100,20);
b3=new JButton("发送文件");
add(b3);
b3.addActionListener(this);
b3.setBounds(510,230,100,20);
a4=new JTextArea();
add(a4);
a4.setBounds(430,260,200,55);
a4.setEditable(false);
a4.setBackground(null);
//a4.setVisible(false);
//add(j5);
//j5.setBounds(430,260,200,100);
b4=new JButton("是");
add(b4);
b4.addActionListener(this);
b4.setBounds(430,320,60,20);
b5=new JButton("否");
add(b5);
b5.addActionListener(this);
b5.setBounds(520,320,60,20);
b4.setVisible(false);
b5.setVisible(false);
/*fc=new JFileChooser();
add(fc);
fc.setBounds(400, 200, 200, 200);*/
}
/* try
{
Socket client_socket=new Socket(opip,opport);
out=new PrintWriter(client_socket.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
//System.out.println(in.readLine());
//if(!in.readLine().equals(""))
a1.append(tit+":"+in.readLine()+"\n");
//BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
}
catch (Exception e1)
{
System.out.println(e1);
}*/
//System.out.println(in.readLine()); void sendmsg()
{
try
{
Socket myClient=new Socket(opip,opport);
out=new PrintWriter(myClient.getOutputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(myClient.getInputStream()));
//System.out.println(in.readLine());
//if(!in.readLine().equals(""))
while(true)
{
//if(xx.win==false)
// new chatinter(opip,opport,tit,myname,xx).start();
String talk=in.readLine();
System.out.println("收到消息:"+talk);
a1.setCaretPosition(a1.getText().length());
if(talk.equals("agreetoreceivefileaaaaaaaaaa"))
{
String filename;
long filesize;
filename=in.readLine();
filesize=Long.parseLong(in.readLine());
int i;
for (i = 0; filesize >= mod; i++) {
filesize /= mod;
}
size=Math.round(filesize*10)/10.0 + units[i];
b4.setVisible(true);
b5.setVisible(true);
a4.append(tit+"希望向您发送文件:"+"\n"+filename+",大小:"+size+","+"\n"+"是否接收?");
//a4.append(tit+"希望向您发送文件:"+"\n"+",大小:"+size+","+"\n"+"是否接收?");
}
else if(talk.equals("afsdghfdhfadhgsafg"))
a1.append("对方拒绝接受文件"+"\n");
else if(talk.equals("qwetqeryfgnhdwfgyqwet"))
new sendfile(fc,myClient).start();
else if(!(talk.equals("")))
a1.append(tit+":"+talk+"\n");
a1.setCaretPosition(a1.getText().length());
}
//BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
}
catch (Exception e1)
{
System.out.println(e1);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
try
{
String msg=this.a2.getText();
a1.setCaretPosition(a1.getText().length());
if(!msg.equals(""))
a1.append("我:"+msg+"\n");
a1.setCaretPosition(a1.getText().length());
//PrintWriter out=new PrintWriter(client_socket.getOutputStream());
//BufferedReader in=new BufferedReader(new InputStreamReader(client_socket.getInputStream()));
//BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
//String str=a2.getText();
a2.setText("");
if(sendname==0)
{
out.println(myname);
out.flush();
sendname++;
}
out.println(msg);
out.flush();
}
catch (Exception e1)
{
System.out.println(e1);
}
}
else if(e.getSource()==b2)
{
fc = new JFileChooser();
int rs =fc.showOpenDialog(this);
if (JFileChooser.APPROVE_OPTION == rs)
{
//JOptionPane.showMessageDialog(this, "确定!");
t1.setText(fc.getSelectedFile().getAbsolutePath());
//System.out.println(fc.getSelectedFile().getName());
//System.out.println(fc.getSelectedFile().length());
//double bytesize=fc.getSelectedFile().length();
//System.out.println(size);
}
}
else if(e.getSource()==b3)
{
try
{
out2 = new DataOutputStream(new BufferedOutputStream(myClient.getOutputStream()));
in2 = new DataInputStream(new BufferedInputStream(myClient.getInputStream()));
out2.writeBytes("agreetoreceivefileaaaaaaaaaa"+"\n");
out2.flush();
out2.write((fc.getSelectedFile().getName()+"\n").getBytes());
out2.flush();
out2.writeBytes(fc.getSelectedFile().length()+"\n");
out2.flush();
}
catch(IOException e1){e1.printStackTrace();}
finally
{
/*try {
out2.close();
in2.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
}
}
else if(e.getSource()==b4)
{
a1.append("开始接受文件\n");
out.println("qwetqeryfgnhdwfgyqwet");
out.flush();
a4.setText("");
b4.setVisible(false);
b5.setVisible(false);
new receivefile(fc,myClient,cc).start();
}
else if(e.getSource()==b5)
{
a1.append("拒绝接受文件\n");
out.println("afsdghfdhfadhgsafg");
out.flush();
a4.setText("");
b4.setVisible(false);
b5.setVisible(false);
}
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e)
{
/*if(cw==1)
xx.win=false;
if(cw==2)
xx.winopen=false;*/
System.out.println("*********closing**************");
}
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("*********closed**************");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
根据楼主的描述,楼主需要的是界面关闭,但是socket不断开。
在socket接受消息中保存界面的引用,当收到消息,检查window.isopen(),就可以了。
思路大致如此,困难是什么?
意思就是说,你少了一行addWindowListener(),所以虽然触发了windowClosed和windowClosing事件,但是没有找到处理的人,你看起来就像没有触发一样。
通常在事件处理代码比较少的时候,你可以使用匿名内部类的方式去实现WindowListener接口(就像我代码里边只是输出了一个字符串)。
而如果处理代码比较多,你就可以单独建一个类来实现WindowListener接口,然后在JFrame构造方法里边加一句addWindowListener(new MyListener());这样,在事件触发的时候,就会到这个Listener里边找处理方法。然后你最开始的问题就好解决了。