今天小编给大家分享一下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。