Java实现带图形界面的聊天程序
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了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();
}
}
运行截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341