Edit Page First
Package Com.lovo.feichun;
Import Java.awt.Color;
Import java.awt.Component;
Import Java.awt.Container;
Import Java.awt.Toolkit;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.Properties;
Import Javax.swing.AbstractButton;
Import Javax.swing.JButton;
Import Javax.swing.JCheckBox;
Import Javax.swing.JComboBox;
Import Javax.swing.JFrame;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
Import javax.swing.text.JTextComponent;
public class Feichunframe extends jframe{
Private Container CONTENTP;
Private JTextArea msgarea;//Text field
Private JButton FASONGJBT;
Private JButton PINGCHUJBT;
text box
Private JTextField Dizhitext;
Private JTextField Shurutext;
Private Object fromclient;
Private JComboBox Xiacomb;
Private Properties ppt;
Private Jcheckbox CHOOSEJBX;
Private JTextField Huifutext;
Public Feichunframe () {
ppt = new Properties ();
try {
Ppt.load (New FileInputStream ("J124.properties"));
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
TODO auto-generated Constructor stub
Toolbox
Toolkit tk = Toolkit.getdefaulttoolkit ();
Form size
This.setsize (400, 400);
int screenw = (int) tk.getscreensize (). GetWidth ();
int screenh = (int) tk.getscreensize (). GetHeight ();
This.setlocation ((screenW-400)/2, (screenH-400)/2);
Whether the form size can be changed
This.setresizable (TRUE);
//
This.seticonimage (Tk.createimage ("Img/logo. GIF "));
Title
This.settitle ("My flying Spring");
//
This.setdefaultcloseoperation (Jframe.exit_on_close);
Content Panel
This.addcontent ();
Form visibility
This.setvisible (TRUE);
Jianting ();
}
Public JComboBox Getxiacomb () {
return xiacomb;
}
public void Setxiacomb (JComboBox xiacomb) {
This.xiacomb = Xiacomb;
}
Public Properties getppt () {
return ppt;
}
public void Setppt (Properties ppt) {
this.ppt = ppt;
}
private void Addcontent () {
TODO auto-generated Method Stub
//
THIS.CONTENTP = This.getcontentpane ();
Empty layout
This.contentP.setLayout (NULL);
This.msgarea = new JTextArea ();
This.setbackground (Color.White);
This.msgArea.setEnabled (FALSE);
JScrollPane sp = new JScrollPane (This.msgarea);
Sp.setbounds (5, 5, 370, 270);
This.contentP.add (SP);
This. Shurutext = new JTextField ("");
This. Shurutext.setbounds (10, 280, 100, 25);
This.contentP.add (this. Shurutext);
This. FASONGJBT = new JButton ("send");
This. Fasongjbt.setbounds (120, 280, 60, 25);
This.contentP.add (this. FASONGJBT);
This. Fasongjbt.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent arg0) {
TODO auto-generated Method Stub
Date D=new date ();
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String Time=sdf.format (d);
String Str=feichunframe.this.shurutext.gettext ();
String Ip=feichunframe.this.ppt.getproperty (str);
String Sendip = FeiChunFrame.this.ShuRuText.getText ();
FeiChunFrame.this.msgArea.setText (time+ "\ n" + "I said" +str);
Socket client = null;
try {
Client = new Socket (IP, 9527);
BufferedWriter bw = new BufferedWriter (New OutputStreamWriter (Client.getoutputstream ()));
Bw.write ("Wang Haijun &" + "&" + str);
Bw.flush ();
} catch (Unknownhostexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
} catch (IOException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}finally{
if (client!=null) {
try {
Client.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
});
This. Dizhitext = new JTextField ("");
This. Dizhitext.setbounds (190, 280, 80, 25);
This.contentP.add (this. Dizhitext);
THIS.CHOOSEJBX = new Jcheckbox ("Auto reply");
This.chooseJbx.setBounds (10, 310, 100, 25);
This.contentP.add (THIS.CHOOSEJBX);
This.huifutext = new JTextField ("");
This.huiFuText.setBounds (130, 310, 80, 25);
This.contentP.add (This.huifutext);
This.xiacomb = new JComboBox (This.ppt.keySet (). ToArray ());
This.xiaComb.setBounds (190, 280, 80, 25);
This.contentP.add (This.xiacomb);
This. PINGCHUJBT = new JButton ("clear");
This. Pingchujbt.setbounds (280, 280, 60, 25);
This.contentP.add (this. PINGCHUJBT);
This. Pingchujbt.addactionlistener (new ActionListener () {
@Override
public void actionperformed (ActionEvent e) {
TODO auto-generated Method Stub
FeiChunFrame.this.msgArea.setText (NULL);
}
});
}
public void jianting () {
ServerSocket server = null;
try {
Server = new ServerSocket (9527);
while (true) {
Socket FC = Server.accept ();
New Msgthread (FC, this). Start ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (server! = null) {
try {
Server.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Public Container getcontentp () {
return CONTENTP;
}
public void Setcontentp (Container contentp) {
THIS.CONTENTP = CONTENTP;
}
Public JTextArea Getmsgarea () {
return msgarea;
}
public void Setmsgarea (JTextArea msgarea) {
This.msgarea = Msgarea;
}
Public JButton GETFASONGJBT () {
return FASONGJBT;
}
public void Setfasongjbt (JButton fasongjbt) {
FASONGJBT = FASONGJBT;
}
Public JButton GETPINGCHUJBT () {
return PINGCHUJBT;
}
public void Setpingchujbt (JButton pingchujbt) {
PINGCHUJBT = PINGCHUJBT;
}
Public JTextField Getdizhitext () {
return dizhitext;
//}
//
public void Setdizhitext (JTextField dizhitext) {
Dizhitext = Dizhitext;
//}
Public JTextField Getshurutext () {
return shurutext;
}
public void Setshurutext (JTextField shurutext) {
Shurutext = Shurutext;
}
Public Object getfromclient () {
return fromclient;
}
public void Setfromclient (Object fromclient) {
This.fromclient = fromclient;
}
}
To listen to messages.
Package Com.lovo.feichun;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Msgthread extends thread{
Private Socket fromclient;
Private Feichunframe Feichun;
Public Msgthread (Socket fromclient,feichunframe Feichun) {
TODO auto-generated Constructor stub
This.fromclient = fromclient;
This.feichun = Feichun;
}
public void Run () {
BufferedReader br= null;
try {
br = new BufferedReader (New InputStreamReader (
Fromclient.getinputstream ()));
String str = br.readline ();
string[] allmsg = Str.split ("&");
Date Sendtime = new Date (Long.parselong (allmsg[2));
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
System.out.println (Sdf.format (sendtime));
System.out.println (Allmsg[0] + "said:" + allmsg[1]);
This.getfeichun (). Getmsgarea (). Append (This.getfeichun (). Getmsgarea (). GetText () + "\ n" + "\ n" +allmsg[0]+ "he said," + ALLMSG[1]);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
if (br! = null) {
try {
Br.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (this.fromclient! = null) {
try {
This.fromClient.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Public Feichunframe Getfeichun () {
return Feichun;
}
public void Setfeichun (Feichunframe Feichun) {
This.feichun = Feichun;
}
}
The Main method runs
Package com.lovo.test;
Import Com.lovo.feichun.FeiChunFrame;
public class Testmain {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Feichunframe ();
}
}
I don't know how to finish it.
A simple chat code similar to QQ