Java實(shí)現(xiàn)帶圖形界面的聊天程序
本文實(shí)例為大家分享了Java實(shí)現(xiàn)帶圖形界面聊天程序的具體代碼,供大家參考,具體內(nèi)容如下
ServerDemo01.java
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo01 implements ActionListener {
? ? private ServerSocket serverSocket;
? ? private Socket socket;
? ? private DataInputStream dataInputStream;
? ? private DataOutputStream dataOutputStream;
? ? private JFrame jFrame;
? ? private JScrollPane jScrollPane;
? ? private JTextArea jTextArea;
? ? private JPanel jPanel;
? ? private JTextField jTextField;
? ? private JButton jButton;
? ? public ServerDemo01() {
? ? ? ? //創(chuàng)建組件
? ? ? ? jFrame = new JFrame("服務(wù)器");
? ? ? ? jTextArea = new JTextArea();
? ? ? ? jScrollPane = new JScrollPane(jTextArea);
? ? ? ? jPanel = new JPanel();
? ? ? ? jTextField = new JTextField(20);
? ? ? ? jButton = new JButton("發(fā)送");
? ? ? ? //添加組件到容器
? ? ? ? jFrame.add(jScrollPane, BorderLayout.CENTER);
? ? ? ? jFrame.add(jPanel, BorderLayout.SOUTH);
? ? ? ? jPanel.add(jTextField);
? ? ? ? jPanel.add(jButton);
? ? ? ? //設(shè)置窗體
? ? ? ? jFrame.setBounds(200,200,400,300);
? ? ? ? jFrame.setVisible(true);
? ? ? ? jFrame.setResizable(false);
? ? ? ? jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? //添加監(jiān)聽
? ? ? ? jButton.addActionListener(this);
? ? ? ? //TCP網(wǎng)絡(luò)通信
? ? ? ? try {
? ? ? ? ? ? serverSocket = new ServerSocket(9999);
? ? ? ? ? ? socket = serverSocket.accept();//等待連接
? ? ? ? ? ? dataInputStream = new DataInputStream(socket.getInputStream());
? ? ? ? ? ? dataOutputStream = new DataOutputStream(socket.getOutputStream());
? ? ? ? ? ? while(true) {//接收來自客戶端的消息
? ? ? ? ? ? ? ? String strClient = dataInputStream.readUTF();
? ? ? ? ? ? ? ? jTextArea.append("客戶端:" + strClient + "\n");
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? @Override
? ? public void actionPerformed(ActionEvent e) {
? ? ? ? //監(jiān)聽發(fā)送按鈕
? ? ? ? if(e.getSource()==jButton){
? ? ? ? ? ? String str = jTextField.getText();//獲取文本框中的內(nèi)容
? ? ? ? ? ? if(str.length()==0)//發(fā)送內(nèi)容為空
? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(jTextField,"發(fā)送內(nèi)容不能為空");
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? //將數(shù)據(jù)發(fā)送到客戶端
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? dataOutputStream.writeUTF(str);
? ? ? ? ? ? ? ? ? ? jTextArea.append("服務(wù)器:"+str + "\n");
? ? ? ? ? ? ? ? ? ? jTextField.setText("");
? ? ? ? ? ? ? ? } catch (IOException ioException) {
? ? ? ? ? ? ? ? ? ? ioException.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? new ServerDemo01();
? ? }
}ClientDemo01.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ClientDemo01 implements ActionListener {
? ? private Socket socket;
? ? private DataInputStream dataInputStream;
? ? private DataOutputStream dataOutputStream;
? ? private JFrame jFrame;
? ? private JScrollPane jScrollPane;
? ? private JTextArea jTextArea;
? ? private JPanel jPanel;
? ? private JTextField jTextField;
? ? private JButton jButton;
? ? public ClientDemo01() {
? ? ? ? //創(chuàng)建組件
? ? ? ? jFrame = new JFrame("客戶端");
? ? ? ? jTextArea = new JTextArea();
? ? ? ? jScrollPane = new JScrollPane(jTextArea);
? ? ? ? jPanel = new JPanel();
? ? ? ? jTextField = new JTextField(20);
? ? ? ? jButton = new JButton("發(fā)送");
? ? ? ? //添加組件
? ? ? ? jFrame.add(jScrollPane, BorderLayout.CENTER);
? ? ? ? jFrame.add(jPanel,BorderLayout.SOUTH);
? ? ? ? jPanel.add(jTextField);
? ? ? ? jPanel.add(jButton);
? ? ? ? //設(shè)置窗體
? ? ? ? jFrame.setBounds(610,200,400,300);
? ? ? ? jFrame.setVisible(true);
? ? ? ? jFrame.setResizable(false);
? ? ? ? jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
? ? ? ? //添加監(jiān)聽
? ? ? ? jButton.addActionListener(this);
? ? ? ? //TCP網(wǎng)絡(luò)通信
? ? ? ? try {
? ? ? ? ? ? socket = new Socket("127.0.0.1",9999);
? ? ? ? ? ? dataInputStream = new DataInputStream(socket.getInputStream());
? ? ? ? ? ? dataOutputStream = new DataOutputStream(socket.getOutputStream());
? ? ? ? ? ? //接收服務(wù)器的消息
? ? ? ? ? ? while (true) {
? ? ? ? ? ? ? ? String strServer = dataInputStream.readUTF();
? ? ? ? ? ? ? ? jTextArea.append("服務(wù)器:"+strServer + "\n");
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? @Override
? ? public void actionPerformed(ActionEvent e) {
? ? ? ? if(e.getSource()==jButton){
? ? ? ? ? ? String str = jTextField.getText();
? ? ? ? ? ? if(str.length()==0)
? ? ? ? ? ? ? ? JOptionPane.showMessageDialog(jTextField,"發(fā)送內(nèi)容不能為空");
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? dataOutputStream.writeUTF(str);
? ? ? ? ? ? ? ? ? ? jTextArea.append("客戶端:"+str + "\n");
? ? ? ? ? ? ? ? ? ? jTextField.setText("");
? ? ? ? ? ? ? ? } catch (IOException ioException) {
? ? ? ? ? ? ? ? ? ? ioException.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? new ClientDemo01();
? ? }
}運(yùn)行截圖:


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁查詢功能
這篇文章主要介紹了SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁查詢功能,pringBoot分頁查詢的兩種寫法,一種是手動(dòng)實(shí)現(xiàn),另一種是使用框架實(shí)現(xiàn),現(xiàn)在我將具體的實(shí)現(xiàn)流程分享一下,需要的朋友可以參考下2023-11-11
elasticsearch分布式及數(shù)據(jù)的功能源碼分析
這篇文章主要為大家介紹了elasticsearch分布式及數(shù)據(jù)功能源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
springboot下使用shiro自定義filter的個(gè)人經(jīng)驗(yàn)分享
這篇文章主要介紹了springboot下使用shiro自定義filter的個(gè)人經(jīng)驗(yàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口
這篇文章主要給大家介紹了關(guān)于Spring Cloud實(shí)戰(zhàn)技巧之使用隨機(jī)端口的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-06-06
基于mybatis-plus QueryWrapper 排序的坑
這篇文章主要介紹了mybatis-plus QueryWrapper 排序的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

