Implementation of Java Chat window
Write a datagram communication program to achieve a simple chat function.
Chat content and input text are the historical information for the current chat and the chat text that is currently being transmitted. Ok, empty, exit three buttons implement the function of sending the current chat text, emptying the current chat text, and exiting the system respectively. Import Java.awt.Font;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import java.awt.event.WindowEvent;
Import Java.awt.event.WindowListener;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
Import java.net.SocketException;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JScrollBar;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
Import Javax.swing.JTextField;
public class Frame extends JFrame implements windowlistener{
private JTextArea text;
Private JTextField Iptext;
Private JTextField SendText;
Private JButton button;
Private JButton button1;
Private JButton button2;
Private Datagramsocket socket;
Private Jscrollbar Vsbar;
Public Frame () {
Settitle ("chatroom");
SetBounds (100, 150,481, 371);
Text=new JTextArea ();
Text.seteditable (TRUE);
SetLayout (NULL);
JScrollPane Textpanel = new JScrollPane (text);
Vsbar = Textpanel.getverticalscrollbar ();
Textpanel.setbounds (10,10, 320, 240);
Getcontentpane (). Add (Textpanel);
JLabel label=new JLabel ("Please enter each other's IP:");
Label.setfont (New Font ("", font.bold,14));
Label.setbounds (342, 24, 110, 24);
Getcontentpane (). Add (label);
Iptext = new JTextField ();
Iptext.setbounds (352, 54, 111, 31);
Getcontentpane (). Add (Iptext);
Button=new JButton ();
Button.settext ("OK");
Button.setbounds (363, 135, 85, 47);
Button.setfont (New Font ("", font.bold,23));
Getcontentpane (). Add (button);
Button.addactionlistener (new Send ());
Button1=new JButton ("Empty");
Button1.setbounds (363, 200, 85, 47);
Button1.setfont (New Font ("", font.bold,23));
Getcontentpane (). Add (button1);
Button1.addactionlistener (new Clear ());
Button2=new JButton ("exit");
Button2.setbounds (363, 260, 85, 47);
Button2.setfont (New Font ("", font.bold,23));
Getcontentpane (). Add (Button2);
Button2.addactionlistener (New exit ());
This.addwindowlistener (this);
SendText = new JTextField ();
Sendtext.setbounds (10, 260, 320, 47);
Getcontentpane (). Add (SendText);
Server ();
Pack ();
SetVisible (TRUE);
}
Class Send implements actionlistener{
public void actionperformed (ActionEvent e) {
try{
String Ip=iptext.gettext ();
inetaddress address=inetaddress.getbyname (IP);
Byte[] Data=sendtext.gettext (). GetBytes ();
Datagrampacket dp=new Datagrampacket (data,data.length,address,9527);
String myip=inetaddress.getlocalhost (). gethostaddress ();
Text.append (myip+ ": N" +sendtext.gettext () + "n");
Socket.send (DP);
Sendtext.settext (NULL);
}catch (Exception E1) {
SYSTEM.OUT.PRINTLN (E1);
}
}
}
Class Clear Implements Actionlistener{
public void actionperformed (ActionEvent e) {
Text.settext ("");
}
}
Class Exit Implements Actionlistener{
public void actionperformed (ActionEvent e) {
System.exit (0);
}
}
private void Server () {
try {
Socket=new Datagramsocket (9527);
Byte[] Buf=new byte[1024];
Final Datagrampacket dp1=new Datagrampacket (buf,buf.length);
Runnable runnable=new Runnable () {
public void Run () {
while (true) {
try{
Thread.Sleep (100);
Socket.receive (DP1);
String Message=new string (Dp1.getdata (), 0,dp1.getlength ());
String ip=dp1.getaddress (). gethostaddress ();
if (! Inetaddress.getlocalhost (). Gethostaddress (). Equals (IP))
Text.append (ip+ ": n" +message+ "n");
}catch (Exception e) {
System.out.println (e);
}
}
}
};
New Thread (runnable). Start ();
catch (SocketException E1) {
E1.printstacktrace ();
}
}
public static void Main (string[] args) {
Frame frame=new frame ();
}
public void windowactivated (WindowEvent e) {
TODO auto-generated Method Stub
}
public void windowclosed (WindowEvent e) {
TODO auto-generated Method Stub
}
public void windowclosing (WindowEvent e) {
TODO auto-generated Method Stub
System.exit (0);
}
public void windowdeactivated (WindowEvent e) {
TODO auto-generated Method Stub
}
public void windowdeiconified (WindowEvent e) {
TODO auto-generated Method Stub
}
public void windowiconified (WindowEvent e) {
TODO auto-generated Method Stub
}
public void windowopened (WindowEvent e) {
TODO auto-generated Method Stub
}
}