package cn.edu.ldu;
import cn.edu.ldu.util.Message;
import cn.edu.ldu.util.Translate;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
/**
* ClientUI,客户机聊天界面类
* @author 南京农业大学 19215227 计科152 程柯
*/
public class ClientUI extends javax.swing.JFrame {
private DatagramSocket clientSocket; //客户机套接字
private Message msg; //消息对象
private byte[] data=new byte[8096]; //8K字节数组
/**
* Creates new form ClientUI
*/
public ClientUI() {
initComponents();
//设置窗体位置到屏幕中央
int x = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width)/2;
int y = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height)/2;
this.setLocation(x, y);
filePanel.setVisible(false);
}
/**
* 构造函数
* @param socket 客户机会话套接字
* @param msg 登录消息对象
*/
public ClientUI(DatagramSocket socket,Message msg) {
this(); //调用无参数构造函数,初始化界面
clientSocket=socket; //初始化会话套接字
this.msg=msg; //登录消息
//创建客户机消息接收和处理线程
Thread recvThread=new ReceiveMessage(clientSocket,this);
recvThread.start();//启动消息线程
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
fileMenu = new javax.swing.JPopupMenu();
uploadFile = new javax.swing.JMenuItem();
jScrollPane2 = new javax.swing.JScrollPane();
txtInput = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
userList = new javax.swing.JList<>();
filePanel = new javax.swing.JPanel();
fileLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
jScrollPane1 = new javax.swing.JScrollPane();
txtArea = new javax.swing.JTextArea();
btnSend = new javax.swing.JButton();
btnFile = new javax.swing.JButton();
jScrollPane4 = new javax.swing.JScrollPane();
fileList = new javax.swing.JList<>();
uploadFile.setText("上传文件");
uploadFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
uploadFileActionPerformed(evt);
}
});
fileMenu.add(uploadFile);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("发言窗口"));
txtInput.setColumns(20);
txtInput.setRows(5);
jScrollPane2.setViewportView(txtInput);
jScrollPane3.setBorder(javax.swing.BorderFactory.createTitledBorder("在线用户"));
jScrollPane3.setViewportView(userList);
fileLabel.setText("上传进度:");
progressBar.setStringPainted(true);
javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
filePanel.setLayout(filePanelLayout);
filePanelLayout.setHorizontalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(fileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(95, 158, Short.MAX_VALUE))
.addGroup(filePanelLayout.createSequentialGroup()
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
filePanelLayout.setVerticalGroup(
filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(filePanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(fileLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35))
);
jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("会话消息窗口"));
txtArea.setColumns(20);
txtArea.setRows(5);
jScrollPane1.setViewportView(txtArea);
btnSend.setText("发送");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
btnFile.setText("文件传输");
btnFile.setFocusable(false);
btnFile.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
btnFile.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
btnFile.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
btnFileMousePressed(evt);
}
});
btnFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnFileActionPerformed(evt);
}
});
jScrollPane4.setBorder(javax.swing.BorderFactory.createTitledBorder("服务器文件列表(单击下载)"));
fileList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
fileListMouseClicked(evt);
}
});
jScrollPane4.setViewportView(fileList);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(btnFile)
.addGap(30, 30, 30)
.addC