您好,登錄后才能下訂單哦!
今天小編給大家分享一下Java如何實現帶圖形界面的聊天程序的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
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() { //創建組件 jFrame = new JFrame("服務器"); jTextArea = new JTextArea(); jScrollPane = new JScrollPane(jTextArea); jPanel = new JPanel(); jTextField = new JTextField(20); jButton = new JButton("發送"); //添加組件到容器 jFrame.add(jScrollPane, BorderLayout.CENTER); jFrame.add(jPanel, BorderLayout.SOUTH); jPanel.add(jTextField); jPanel.add(jButton); //設置窗體 jFrame.setBounds(200,200,400,300); jFrame.setVisible(true); jFrame.setResizable(false); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加監聽 jButton.addActionListener(this); //TCP網絡通信 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) { //監聽發送按鈕 if(e.getSource()==jButton){ String str = jTextField.getText();//獲取文本框中的內容 if(str.length()==0)//發送內容為空 JOptionPane.showMessageDialog(jTextField,"發送內容不能為空"); else{ //將數據發送到客戶端 try { dataOutputStream.writeUTF(str); jTextArea.append("服務器:"+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() { //創建組件 jFrame = new JFrame("客戶端"); jTextArea = new JTextArea(); jScrollPane = new JScrollPane(jTextArea); jPanel = new JPanel(); jTextField = new JTextField(20); jButton = new JButton("發送"); //添加組件 jFrame.add(jScrollPane, BorderLayout.CENTER); jFrame.add(jPanel,BorderLayout.SOUTH); jPanel.add(jTextField); jPanel.add(jButton); //設置窗體 jFrame.setBounds(610,200,400,300); jFrame.setVisible(true); jFrame.setResizable(false); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加監聽 jButton.addActionListener(this); //TCP網絡通信 try { socket = new Socket("127.0.0.1",9999); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutputStream = new DataOutputStream(socket.getOutputStream()); //接收服務器的消息 while (true) { String strServer = dataInputStream.readUTF(); jTextArea.append("服務器:"+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,"發送內容不能為空"); else{ try { dataOutputStream.writeUTF(str); jTextArea.append("客戶端:"+str + "\n"); jTextField.setText(""); } catch (IOException ioException) { ioException.printStackTrace(); } } } } public static void main(String[] args) { new ClientDemo01(); } }
運行截圖:
以上就是“Java如何實現帶圖形界面的聊天程序”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。