Java instant online chat (1)

Source: Internet
Author: User

Java instant online chat (1)

 

Today, I imitated the video of Jack Ma's instructor and made an instant online chat. At this time, I will share it with you. It is not very good. Please join us.

 
import java.awt.Frame;public class ChatClient extends Frame {/** * @param args */public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);setVisible(true);}}

The above is just a simple layout with no substantive code. The second part is as follows:

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;public class ChatClient extends Frame {TextField tftext=new TextField();TextArea taContent=new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame(){this.setLocation(400, 400);this.setSize(300, 300);add(tftext,BorderLayout.SOUTH);add(taContent,BorderLayout.NORTH);pack();setVisible(true);}}


The second time is an additional input box than the first time. In this case, you can only use the red button in the lower right corner to close it.

Small Red Square


Chat0.3

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});setVisible(true);}}

Compared with Chat0.2, Chat0.3 can be disabled only by clicking "X. No longer.

Chat0.4

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});tftext.addActionListener(new TFListener());setVisible(true);}private class TFListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String s=tftext.getText().trim();taContent.setText(s);tftext.setText("");}}}


After entering the content in the input box, you can press the Enter key to send the message directly.

Chat0.5 has one more server than 0.4. Let's talk about the code.

import java.io.IOException;import java.net.ServerSocket;public class ChatServer {public static void main(String[] args) {try {ServerSocket ss=new ServerSocket(8888);} catch (IOException e) {e.printStackTrace();System.out.println("a client connected");}}}

ChatClient. java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});tftext.addActionListener(new TFListener());setVisible(true);}private class TFListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String s=tftext.getText().trim();taContent.setText(s);tftext.setText("");}}}
Chat0.6

ChatServer. java

import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {public static void main(String[] args) {try {ServerSocket ss=new ServerSocket(8888);while(true){Socket s=ss.accept();System.out.println("a client connected");}} catch (IOException e) {e.printStackTrace();}}}

ChatClient. java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});tftext.addActionListener(new TFListener());setVisible(true);connect();}public void connect(){try {Socket s=new Socket("127.0.0.1",8888);System.out.println("connect");} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private class TFListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String s=tftext.getText().trim();taContent.setText(s);tftext.setText("");}}}


Chat0.7


ChatServer. java

import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {public static void main(String[] args) {try {ServerSocket ss=new ServerSocket(8888);while(true){Socket s=ss.accept();System.out.println("a client connected");            DataInputStream dis=new DataInputStream(s.getInputStream());            String str=dis.readUTF();            dis.close();            System.out.println(str);}} catch (IOException e) {e.printStackTrace();}}}

ChatClient. java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {Socket s=null;TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});tftext.addActionListener(new TFListener());setVisible(true);connect();}public void connect(){try {   s=new Socket("127.0.0.1",8888);System.out.println("connect");} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}private class TFListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String str=tftext.getText().trim();taContent.setText(str);tftext.setText("");try {DataOutputStream dos=new DataOutputStream(s.getOutputStream());dos.writeUTF(str);dos.flush();dos.close();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}}
Chat0.8

ChatServer. java

import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {public static void main(String[] args) {Boolean started=false;try {ServerSocket ss=new ServerSocket(8888);started=true;while(started){ Boolean bconnect=false;Socket s=ss.accept();System.out.println("a client connected");             bconnect=true;            DataInputStream dis=new DataInputStream(s.getInputStream());                          while(bconnect){            String str=dis.readUTF();            System.out.println(str);            }            dis.close();            }} catch (IOException e) {e.printStackTrace();}}}

ChatClient. java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {Socket s=null;DataOutputStream dos=null;TextField tftext = new TextField();TextArea taContent = new TextArea();public static void main(String[] args) {new ChatClient().lanchFrame();}public void lanchFrame() {this.setLocation(400, 400);this.setSize(300, 300);add(tftext, BorderLayout.SOUTH);add(taContent, BorderLayout.NORTH);pack();this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {disconnect();System.exit(0);}});tftext.addActionListener(new TFListener());setVisible(true);connect();}public void connect(){try {   s=new Socket("127.0.0.1",8888);   dos=new DataOutputStream(s.getOutputStream());System.out.println("connect");} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void disconnect(){try {dos.close();s.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private class TFListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {String str=tftext.getText().trim();taContent.setText(str);tftext.setText("");try {dos.writeUTF(str);dos.flush();//dos.close();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}}

Final



You can enter multiple, and the Service can receive multiple.

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.