Java實現帶圖形界面的聊天程序
本文實例為大傢分享瞭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(); } }
運行截圖:
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。