当前位置: 技术问答>java相关
actionPerformed为什么在我点击connect按钮的时候不响应阿?大虾高手帮忙阿?
来源: 互联网 发布时间:2015-02-24
本文导语: import java.awt.*; import java.io.*; import java.net.*; import java.applet.*; import java.awt.event.*; public class Client extends Applet implements ActionListener { public TextArea chat_txt; private TextField sql_txt; private Button connect,execute; p...
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;
public class Client extends Applet implements ActionListener
{
public TextArea chat_txt;
private TextField sql_txt;
private Button connect,execute;
private Socket soc= null;
private PrintWriter ps= null;
Listen listen;
public void init()
{
chat_txt= new TextArea();
sql_txt= new TextField(20);
connect= new Button("Connect");
execute= new Button("Execute");
execute.disable();
Panel pp= new Panel();
pp.setLayout(new FlowLayout());
pp.add(sql_txt);
pp.add(connect);
pp.add(execute);
add("North",pp);
add("Center",chat_txt);
}
public void actionPerformed(ActionEvent ev)
{
String label= ev.getActionCommand();
if(label.equals("Connect"))
{
try{
soc= new Socket(InetAddress.getLocalHost(),4700);
ps= new PrintWriter(soc.getOutputStream());
ps.println("HELLO");
ps.flush();
listen= new Listen(this,soc);
listen.start();
}catch(Exception e)
{
chat_txt.setText(e.toString());
disconnect();
}
connect.setLabel("Disconnect");
execute.enable();
}else if(label.equals("Disconnect"))
disconnect();
else if(label.equals("Execute"))
{
if(sql_txt.getText()!= null)
{
ps.println("FIND");
ps.flush();
ps.println(sql_txt.getText());
ps.flush();
}
}
}
public void disconnect()
{
if(soc!= null)
{
try{
listen.sleep(2000);
listen= null;
ps.println("QUIT");
ps.flush();
soc.close();
soc= null;
}catch(Exception e){}
execute.disable();
connect.setLabel("Disconnect");
}
}
}
class Listen extends Thread
{
Socket socket= null;
BufferedReader dis= null;
Client parent= null;
public Listen(Client p,Socket s)
{
parent= p;
socket= s;
try{
dis= new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(Exception e){}
}
public void run()
{
String line= null;
while(true)
{
try{
line= dis.readLine();
}catch(Exception e){}
if(line!= null)
parent.chat_txt.setText(parent.chat_txt.getText()+line);
}
}
}
import java.io.*;
import java.net.*;
import java.applet.*;
import java.awt.event.*;
public class Client extends Applet implements ActionListener
{
public TextArea chat_txt;
private TextField sql_txt;
private Button connect,execute;
private Socket soc= null;
private PrintWriter ps= null;
Listen listen;
public void init()
{
chat_txt= new TextArea();
sql_txt= new TextField(20);
connect= new Button("Connect");
execute= new Button("Execute");
execute.disable();
Panel pp= new Panel();
pp.setLayout(new FlowLayout());
pp.add(sql_txt);
pp.add(connect);
pp.add(execute);
add("North",pp);
add("Center",chat_txt);
}
public void actionPerformed(ActionEvent ev)
{
String label= ev.getActionCommand();
if(label.equals("Connect"))
{
try{
soc= new Socket(InetAddress.getLocalHost(),4700);
ps= new PrintWriter(soc.getOutputStream());
ps.println("HELLO");
ps.flush();
listen= new Listen(this,soc);
listen.start();
}catch(Exception e)
{
chat_txt.setText(e.toString());
disconnect();
}
connect.setLabel("Disconnect");
execute.enable();
}else if(label.equals("Disconnect"))
disconnect();
else if(label.equals("Execute"))
{
if(sql_txt.getText()!= null)
{
ps.println("FIND");
ps.flush();
ps.println(sql_txt.getText());
ps.flush();
}
}
}
public void disconnect()
{
if(soc!= null)
{
try{
listen.sleep(2000);
listen= null;
ps.println("QUIT");
ps.flush();
soc.close();
soc= null;
}catch(Exception e){}
execute.disable();
connect.setLabel("Disconnect");
}
}
}
class Listen extends Thread
{
Socket socket= null;
BufferedReader dis= null;
Client parent= null;
public Listen(Client p,Socket s)
{
parent= p;
socket= s;
try{
dis= new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch(Exception e){}
}
public void run()
{
String line= null;
while(true)
{
try{
line= dis.readLine();
}catch(Exception e){}
if(line!= null)
parent.chat_txt.setText(parent.chat_txt.getText()+line);
}
}
}
|
你的Button connect没有调用addListener()方法,这个都能忘记,你真行