package实验五聊天;importjava.awt.BorderLayout;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.border.EmptyBorder;importjavax.swing.JLabel;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.JButton;importjavax.swing.AbstractAction;importjava.awt.event.ActionEvent;importjavax.swing.Action;importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.InetAddress;importjava.net.ServerSocket;importjava.net.Socket;importjava.net.UnknownHostException;public class 客户端 extends JFrame implementsRunnable{privateJPanel contentPane;privateJTextField textField;privateJButton button;private staticJTextArea BtextArea;public static ServerSocket serverSocket=null;public static Socket clientSocket=null;public Socket client=null;public DataOutputStream out=null;public static DataInputStream in=null;privateThread thread;public voidrun()
{try{while(true)
{if(in!=null)
{
String tmp=in.readUTF();
BtextArea.append("\n服务端:\n"+tmp);
}
}
}catch(Exception e) {
e.printStackTrace();
}
}/*** Launch the application.*/
public static voidmain(String[] args) {
EventQueue.invokeLater(newRunnable() {public voidrun() {try{
客户端 frame= new客户端();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//frame.setTitle("客户端");
frame.setVisible(true);
}catch(Exception e) {
e.printStackTrace();
}
}
});
}public voidconnect()
{
InetAddress localIP=null;try{if(client==null)
{
localIP=InetAddress.getLocalHost();
client=new Socket(localIP,8000);
out=newDataOutputStream(client.getOutputStream());
in=newDataInputStream(client.getInputStream());if(!(thread.isAlive()))
{
thread=new Thread(this);
}
thread.start();
}
}catch(UnknownHostException e2) {//TODO 自动生成的 catch 块
e2.printStackTrace();
}catch(IOException e2)
{
e2.printStackTrace();
}
}/*** Create the frame.*/
public客户端() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane= newJPanel();
setTitle("客户端");
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea textArea= newJTextArea();
textArea.setBounds(0, 0, 299, 132);
BtextArea=textArea;
JScrollPane jsp= new JScrollPane(textArea); //在文本框上添加滚动条
jsp.setBounds(0, 15, 350, 100);//默认的设置是超过文本框才会显示滚动条,以下设置让滚动条一直显示
jsp.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(jsp);
textField= newJTextField();
textField.setBounds(10, 127, 257, 102);
contentPane.add(textField);
textField.setColumns(10);
button= new JButton("\u53D1\u9001");
button.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
String tmp=textField.getText();
textField.setText("");
BtextArea.append("\n客户端:"+tmp);try{
out.writeUTF(tmp);
}catch(Exception e1)
{
e1.printStackTrace();
}
}
});
button.setBounds(282, 185, 123, 29);
contentPane.add(button);
JButton button_1= new JButton("\u8FDE\u63A5");
button_1.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {
connect();
}
});
button_1.setBounds(282, 130, 123, 29);
contentPane.add(button_1);
thread=new Thread(this);
}
}