Using Java to implement simple chat applet

Source: Internet
Author: User
Tags gettext

The applet currently does not have a dialog function, only the most basic login, the function of displaying the chat record


Login interface
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JButton;
Import Javax.swing.JTextField;
Import Javax.swing.JPasswordField;
Import Javax.swing.JPanel;
Import Java.awt.GridLayout;
Import Java.awt.BorderLayout;
Import Java.awt.FlowLayout;
public class Qqlogin extends jframe{
Private JLabel Labuser; User tags
Private JLabel Labpass; Password tags
Private JTextField Txtuser; User Information Entry Box
Private JPasswordField Txtpass; User Password entry box
Private JButton Btnreg; Register button
Private JButton Btnlogin; Login button
Private JButton Btncancel; Cancel button

Private JPanel Jpcenter; Central layout
Private JPanel Jpsouth; Southern Layout
Public Qqlogin () {
Init ();
This.setsize (250,125);
This.setresizable (FALSE);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.setlocation (300,100);
}
Interface layout
public void init () {
User tags and password tags
Labuser=new JLabel ("user");
Labpass=new JLabel ("password");
User and Password entry boxes
Txtuser=new JTextField ();
Txtpass=new JPasswordField ();
Button
Btnreg=new JButton ("registration");
Btnlogin=new JButton ("login");
Btncancel=new JButton ("Cancel");

Central layout
Jpcenter=new JPanel ();
Jpcenter.setlayout (New GridLayout (2,2));
Jpcenter.add (Labuser);
Jpcenter.add (Txtuser);
Jpcenter.add (Labpass);
Jpcenter.add (Txtpass);
This.add (Jpcenter,borderlayout.center);

Southern Layout
Jpsouth=new JPanel ();
Jpsouth.setlayout (New FlowLayout ());
Jpsouth.add (Btnreg);
Jpsouth.add (Btnlogin);
Jpsouth.add (Btncancel);
This.add (Jpsouth,borderlayout.south);
}
public static void Main (string[] args) {
Qqlogin login=new Qqlogin ();
Login.setvisible (TRUE);
}
} //Registration Interface Import Javax.swing.jframe;import Javax.swing.jlabel;import javax.swing.jbutton;import Javax.swing.joptionpane;import Javax.swing.jtextfield;import Javax.swing.jpasswordfield;import Javax.swing.JPanel ; Import Java.awt.borderlayout;import Java.awt.gridlayout;import Java.awt.event.actionlistener;import Java.awt.event.actionevent;import Java.io.file;import Java.io.filewriter;import Java.io.PrintWriter;public class Qqreg extends JFrame implements actionlistener{JLabel labmess;//Show registration label JLabel labuser;//user tag JLabel labpass;//password tag JT  Extfield txtuser;//user input box jpasswordfield txtpass;//password input box JButton btnok;//confirm button JButton btncancel;//Cancel button JPanel Jpsouth;  JPanel Jpcenter;    Public Qqreg () {init ();    This.setbounds (500,200,300,150);    This.setresizable (FALSE);  This.setdefaultcloseoperation (Jframe.exit_on_close);    } public void Init () {//Instantiate all options labmess=new JLabel ("registration Information");    Labuser=new JLabel ("user");    Labpass=new JLabel ("password");    Txtuser=new JTextField (); TxtpaSs=new JPasswordField ();    Btnok=new JButton ("confirmation");    Btnok.addactionlistener (this);    Btncancel=new JButton ("Cancel");    Btncancel.addactionlistener (This);  jpsouth=new JPanel ();    Jpsouth.add (Btnok);    Jpsouth.add (btncancel);  jpcenter=new JPanel ();    Jpcenter.setlayout (New GridLayout (2,2));    Jpcenter.add (Labuser);    Jpcenter.add (Txtuser);    Jpcenter.add (Labpass);    Jpcenter.add (Txtpass);  this.add (Labmess,borderlayout.north);    This.add (Jpcenter,borderlayout.center); This.add (Jpsouth,borderlayout.south); } @Override public void actionperformed (ActionEvent e) {if (E.getactionco Mmand () = = "Confirm") {try{file Outfile=new file ("F:/java/qqsource.txt");  FileWriter fw=new FileWrite         R (Outfile,true);         PrintWriter pw=new PrintWriter (FW);  String User=txtuser.gettext (); String Pass=new string (Txtpass.getpassword ());  if (! ( User.equals ("") &&pass.equals (""))) {Pw.println (user+ "%" +pass);           Pw.flush ();         This.setvisible (FALSE);        }}catch (Exception E1) {e1.printstacktrace ();      }} if (E.getactioncommand () = = "Cancel") {Txtuser.settext ("");    Txtpass.settext (""); }}//public static void Main (string[] args) {//Qqreg r=new Qqreg ();//R.setvisible (TRUE);//}}//Chat interface Import javax . swing. Jframe;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.JScrollPane; Import Javax.swing.jpanel;import javax.swing.jbutton;import javax.swing.jtextarea;import Javax.swing.JComboBox; Import Java.awt.gridlayout;import java.awt.borderlayout;import java.awt.event.actionlistener;import java.io.File;  Import Java.io.filewriter;import Java.io.printwriter;public class Qqmain extends JFrame implements actionlistener{ Private JTextArea txtsend;//Send message window private JTextArea txtmess;//display Information window private JButton btnsend;//send button go to private JButton btncancel;//Cancel button Private JButton btnmess;//display message button pRivate JComboBox jblist;//Displays the drop-down list of chat objects private JPanel jpsmall;  Private JPanel Jpbig;  private int score=0;  Private Qqchat chat;    Public Qqmain () {init ();    Chat=new Qqchat ();    This.setbounds (300,100,300,400);    This.setdefaultcloseoperation (Jframe.exit_on_close);  This.setresizable (FALSE);    }//Interface layout button public void init () {txtsend=new jtextarea ();    Txtmess=new JTextArea (10,10);    Txtmess.seteditable (False);  btnsend=new JButton ("send");    Btnsend.addactionlistener (this);    Btncancel=new JButton ("Cancel");    Btnmess=new JButton ("Chat history");    Btnmess.addactionlistener (this);    Jblist=new JComboBox ();  jpsmall=new JPanel ();    Jpsmall.add (jblist);    Jpsmall.add (Btnsend);    Jpsmall.add (Btncancel);    Jpsmall.add (btnmess);  jpbig=new JPanel ();    Jpbig.setlayout (New BorderLayout ());    Jpbig.add (Jpsmall,borderlayout.north);    Jpbig.add (Txtsend,borderlayout.center);  this.setlayout (New GridLayout (2,1));    This.add (txtmess); This.add (JPBIG);  } @Override public void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equals ("send")) {if (!txtsend.get        Text (). Equals ("")) {writechat ();        Txtmess.append (Txtsend.gettext () + "\ n");        Chat.getarea (). Append (Txtsend.gettext () + "\ n");      Txtsend.settext ("");      }} if (E.getactioncommand (). Equals ("Chat history")) {score++;      if (score%2==1) {chat.setvisible (true);      }else{chat.setvisible (FALSE); }}} public void Writechat () {try{file Chat=new file ("F:/java/chat.txt");  FileWriter fw=new file      Writer (chat,true);      PrintWriter pw=new PrintWriter (FW);  String S=txtsend.gettext ();      Pw.println (s);    Pw.flush (); }catch (Exception e) {e.printstacktrace (); }}//public static void Main (string[] args) {//Qqmain main=new qqmain ();//Main.setvisible (TRUE);//}}//Chat record interface
Package Qq1;import Javax.swing.jframe;import javax.swing.jtextarea;import java.io.file;import java.io.FileReader;  Import Java.io.bufferedreader;public class Qqchat extends jframe{private JTextArea areamess;    Public Qqchat () {areamess=new jtextarea ();    Areamess.seteditable (FALSE);    Chatfile ();    This.add (areamess);    This.setresizable (FALSE);    This.setdefaultcloseoperation (Jframe.exit_on_close);  This.setbounds (620,100,300,400);;  } public JTextArea Getarea () {return areamess;      } public void Chatfile () {try{file Chat=new file ("F:/java/chat.txt"); FileReader fr=new FileReader (chat);  bufferedreader br=new BufferedReader (FR);  StringBuffer s=new Str      Ingbuffer ();      while (Br.ready ()) {S.append (Br.readline () + "\ n");     } areamess.settext (S.tostring ());     }catch (Exception e) {e.printstacktrace (); }}//public static void Main (string[] args) {//Qqchat chat=new qqchat ();//chat.setvisible (TRUE);//}} 

Implementing a simple chat applet in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.