/*
*简易QQ聊天系统
*客户端
*
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
class QQChatSystem1
{
JFrame qqframe=new JFrame("QQ聊天系统-客户端");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JTextArea sendInfor=new JTextArea(5,26);
JTextArea receiveInfor=new JTextArea(15,25);
JButton send=new JButton("发送");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
JScrollPane scrollpane=new JScrollPane(receiveInfor);//创建一个滚动条窗格,并且将receiveInfor文本域添加到该滚动条窗格中。
JScrollPane scrollpane2=new JScrollPane(sendInfor);//创建一个滚动条窗格,并且将sendInfor文本域添加到该滚动条窗格中。
Send sendlistener=new Send();
Exit exitlistener=new Exit();
Cancel cancellistener=new Cancel();
String i;
Socket s;
InputStream is;
DataInputStream dis;
OutputStream os;
DataOutputStream dos;
QQChatSystem1() throws Exception
{
receiveInfor.setEditable(false);
qqframe.setLayout(new FlowLayout());
panel3.setLayout(new GridLayout(1,3,50,5));
panel1.setBorder(BorderFactory.createTitledBorder("qq消息"));
panel2.setBorder(BorderFactory.createTitledBorder("发送消息"));
qqframe.add(panel1);
qqframe.add(panel2);
qqframe.add(panel3);
panel1.add(scrollpane);
scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平滚动条总是显示。
scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条总是显示。
panel2.add(scrollpane2);
panel3.add(send);
panel3.add(cancel);
panel3.add(exit);
send.addActionListener(sendlistener);
exit.addActionListener(exitlistener);
cancel.addActionListener(cancellistener);
qqframe.setSize(350,550);
qqframe.setVisible(true);
qqframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
qqframe.setLocationRelativeTo(null);
qqframe.setResizable(false);
s=new Socket("127.0.0.1",8888);
os=s.getOutputStream();
dos=new DataOutputStream(os);
is=s.getInputStream();
dis=new DataInputStream(is);
}
class Send implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==(JButton)send)
{
try
{
i=sendInfor.getText();
dos.writeUTF(i);
receiveInfor.append("客户端:"+i+"\n");
sendInfor.setText("");
}
catch (Exception ee)
{
System.out.println("Error....." + ee);
}
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==(JButton)exit)
{
System.exit(0);
dos.close();
s.close();
dis.close();
}
}
catch(Exception ee)
{
}
}
}
class Cancel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==(JButton)cancel)
{
sendInfor.setText("");
sendInfor.requestFocusInWindow();
}
}
}
void receive()throws Exception
{
while(true)
{
receiveInfor.append("服务器端:"+dis.readUTF()+"\n");
}
}
public static void main(String[] args) throws Exception
{
QQChatSystem1 qqchat=new QQChatSystem1();
qqchat.receive();
}
}
/*
*简易QQ聊天系统
*服务器端
*
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;
class QQChatSystem2
{
JFrame qqframe=new JFrame("QQ聊天系统-服务器端");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JTextArea sendInfor=new JTextArea(5,26);
JTextArea receiveInfor=new JTextArea(15,25);
JButton send=new JButton("发送");
JButton cancel=new JButton("取消");
JButton exit=new JButton("退出");
JScrollPane scrollpane=new JScrollPane(receiveInfor);//创建一个滚动条窗格,并且将receiveInfor文本域添加到该滚动条窗格中。
JScrollPane scrollpane2=new JScrollPane(sendInfor);//创建一个滚动条窗格,并且将sendInfor文本域添加到该滚动条窗格中。
Send sendlistener=new Send();
Exit exitlistener=new Exit();
Cancel cancellistener=new Cancel();
String i;
InputStream in;
DataInputStream dis;
OutputStream os;
ServerSocket ss;
Socket s;
DataOutputStream dos;
QQChatSystem2() throws Exception
{
receiveInfor.setEditable(false);
qqframe.setLayout(new FlowLayout());
panel3.setLayout(new GridLayout(1,3,50,5));
panel1.setBorder(BorderFactory.createTitledBorder("qq消息"));
panel2.setBorder(BorderFactory.createTitledBorder("发送消息"));
qqframe.add(panel1);
qqframe.add(panel2);
qqframe.add(panel3);
panel1.add(scrollpane);
scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平滚动条总是显示。
scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);//设置垂直滚动条总是显示。
panel2.add(scrollpane2);
panel3.add(send);
panel3.add(cancel);
panel3.add(exit);
send.addActionListener(sendlistener);
exit.addActionListener(exitlistener);
cancel.addActionListener(cancellistener);
qqframe.setSize(350,550);
qqframe.setVisible(true);
qqframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
qqframe.setLocationRelativeTo(null);
qqframe.setResizable(false);
ss=new ServerSocket(8888);
s=ss.accept();
in=s.getInputStream();
dis=new DataInputStream(in);
os=s.getOutputStream();
dos=new DataOutputStream(os);
}
class Send implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==(JButton)send)
{
try
{
i=sendInfor.getText();
dos.writeUTF(i);
receiveInfor.append("服务器端:"+i+"\n");
sendInfor.setText("");
}
catch(Exception ex)
{
}
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==(JButton)exit)
{
System.exit(0);
s.close();
}
}
catch(Exception ee)
{
}
}
}
class Cancel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==(JButton)cancel)
{
sendInfor.setText("");
sendInfor.requestFocusInWindow();
}
}
}
void receive()throws Exception
{
while(true)
{
receiveInfor.append("客户端:"+dis.readUTF()+"\n");
}
}
public static void main(String[] args) throws Exception
{
QQChatSystem2 qqchat=new QQChatSystem2();
qqchat.receive();
}
}
|
|