import javax.swing.*; |
import java.awt.*; |
import java.net.*; |
import java.awt.*; |
import javax.swing.*; |
import java.io.*; |
import java.awt.event.*; |
|
public class ChatClient{ |
private String name; |
private Socket s; |
private DataInputStream dis; |
private DataOutputStream dos; |
private JFrame jf; |
private JTextArea jta; |
private JTextField jtf; |
private boolean runnable = true ; |
|
public static void main(String args[]){ |
ChatClient cc = new ChatClient(); |
cc.createUI(); |
cc.inputName(); |
cc.connect(); |
cc.createThread(); |
|
} |
public void createUI(){ |
jf = new JFrame( "客户端" ); |
jta = new JTextArea(); |
jta.setEditable( false ); |
jtf = new JTextField(); |
Button send = new Button( "发送" ); |
Panel p = new Panel(); |
p.setLayout( new BorderLayout()); |
p.add(jtf, "Center" ); |
p.add(send, "East" ); |
jf.add(jta, "Center" ); |
jf.add(p, "South" ); |
|
MyClientListener listener = new MyClientListener( this ); |
send.addActionListener(listener); |
jtf.addActionListener(listener); |
jf.addWindowListener( new WindowAdapter(){ |
public void windowClosing(WindowEvent e){ |
ChatClient. this .shutDown(); |
} |
}); |
jf.setSize( 400 , 400 ); |
jf.setLocation( 600 , 0 ); |
jf.setVisible( true ); |
jtf.requestFocus(); |
} |
public void inputName(){ |
String name = JOptionPane.showInputDialog( "Input Your name:" ); |
this .setName(name); |
jf.setTitle(name); |
} |
public void connect(){ |
try { |
s = new Socket( "127.0.0.1" , 9999 ); |
dos = new DataOutputStream(s.getOutputStream()); |
dis = new DataInputStream(s.getInputStream()); |
dos.writeUTF(name); |
} catch (Exception e){ |
e.printStackTrace(); |
} |
} |
public void createThread(){ |
MyClientReader reader = new MyClientReader( this ); |
reader.start(); |
} |
public void stop(){ |
runnable = false ; |
} |
public void shutDown(){ |
try { |
dos.writeUTF( "Bye" ); |
jta.append( "Exit in 5 seconds" ); |
this .stop(); |
Thread.sleep( 5000 ); |
dis.close(); |
dos.close(); |
s.close(); |
} catch (Exception e){ |
e.printStackTrace(); |
} |
System.exit( 0 ); |
} |
|
public boolean getRunnable(){ |
return runnable; |
} |
public void setName(String name){ |
this .name = name; |
} |
public DataInputStream getDataInputStream(){ |
return dis; |
} |
public DataOutputStream getDataOutputStream(){ |
return dos; |
} |
public JTextArea getTextArea(){ |
return jta; |
} |
public JTextField getTextField(){ |
return jtf; |
} |
|
} |
|
class MyClientListener implements ActionListener{ |
private ChatClient client; |
public MyClientListener(ChatClient client){ |
this .client = client; |
} |
public void actionPerformed(ActionEvent e){ |
JTextField jtf = client.getTextField(); |
String info = jtf.getText(); |
try { |
client.getDataOutputStream().writeUTF(info); |
} catch (Exception e1){ |
e1.printStackTrace(); |
} |
if (info.equals( "bye" )){ |
client.shutDown(); |
} |
jtf.setText( "" ); |
jtf.requestFocus(); |
} |
} |
|
class MyClientReader extends Thread{ |
private ChatClient client; |
public MyClientReader(ChatClient client){ |
this .client = client; |
} |
|
public void run(){ |
String info; |
DataInputStream dis = client.getDataInputStream(); |
JTextArea jta = client.getTextArea(); |
try { |
while (client.getRunnable()){ |
info = dis.readUTF(); |
jta.append(info+ "\n" ); |
} |
} catch (Exception e){ |
e.printStackTrace(); |
} |
} |
} |