/*
*编写一个类似简单QQ聊天的应用程序(要求有图形界面)
*
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Socket_Cilent_gui
{
JFrame f=new JFrame("Cilent_QQ");
TextArea ta=new TextArea(17,10);
JTextField jf=new JTextField(15);
JPanel p=new JPanel();
JPanel p1=new JPanel();
JButton b1=new JButton("发送");
JButton b2=new JButton("取消");
//***************************
Socket s;
InputStream is;
DataInputStream dis;
OutputStream os;
DataOutputStream dos;
String st;
Socket_Cilent_gui() throws Exception
{
ta.setEditable(false);
p.setLayout(new BorderLayout());
p.add(ta,BorderLayout.NORTH);
p.add(jf,BorderLayout.CENTER);
p.add(p1,BorderLayout.SOUTH);
p1.add(b1);
p1.add(b2);
f.add(p);
f.setSize(350,400);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.width-200)/2,(d.height-120)/2);
//***********************************
s=new Socket("59.76.147.158",8888);
is=s.getInputStream();
dis=new DataInputStream(is);
os=s.getOutputStream();
dos=new DataOutputStream(os);
//*****************************
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==(JButton)b2)
{
System.exit(0);
s.close();
dis.close();
dos.close();
}
if(e.getSource()==(JButton)b1)
{
st=jf.getText();
dos.writeUTF(st);
ta.append(" Cilent: "+"\n"+" "+st+"\n");
jf.setText("");
jf.requestFocusInWindow();
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
public void Recver_info() throws Exception
{
while(true)
{
ta.append("Server: "+dis.readUTF()+"\n");
}
}
void Listener()
{
MyActionListener ml=new MyActionListener();
b1.addActionListener(ml);
b2.addActionListener(ml);
}
public static void main(String args[]) throws Exception
{
Socket_Cilent_gui ssg=new Socket_Cilent_gui();
ssg.Listener();
ssg.Recver_info();
}
}
/*
*编写一个类似简单QQ聊天的应用程序(要求有图形界面)
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Socket_Server_gui
{
JFrame f=new JFrame("Server_QQ");
TextArea ta=new TextArea(17,10);
JTextField jf=new JTextField(15);
JPanel p=new JPanel();
JPanel p1=new JPanel();
JButton b1=new JButton("发送");
JButton b2=new JButton("取消");
//***************************
Socket s;
ServerSocket ss;
InputStream is;
DataInputStream dis;
OutputStream os;
DataOutputStream dos;
String st;
Socket_Server_gui() throws Exception
{
ta.setEditable(false);
p.setLayout(new BorderLayout());
p.add(ta,BorderLayout.NORTH);
p.add(jf,BorderLayout.CENTER);
p.add(p1,BorderLayout.SOUTH);
p1.add(b1);
p1.add(b2);
f.add(p);
f.setSize(350,400);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.width-200)/2,(d.height-120)/2);
//***********************************
ss=new ServerSocket(8888);
s=ss.accept();
is=s.getInputStream();
dis=new DataInputStream(is);
os=s.getOutputStream();
dos=new DataOutputStream(os);
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==(JButton)b2)
{
System.exit(0);
s.close();
dis.close();
dos.close();
}
if(e.getSource()==(JButton)b1)
{
st=jf.getText();
dos.writeUTF(st);
ta.append(" Server: "+"\n"+" "+st+"\n");
jf.setText("");
jf.requestFocusInWindow();
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}
public void Recver_info() throws Exception
{
while(true)
{
ta.append("Cilent: "+dis.readUTF()+"\n");
}
}
void Listener()
{
MyActionListener ml=new MyActionListener();
b1.addActionListener(ml);
b2.addActionListener(ml);
}
public static void main(String args[]) throws Exception
{
Socket_Server_gui ssg=new Socket_Server_gui();
ssg.Listener();
ssg.Recver_info();
}
} |
|