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();
}
}
运行截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章