📄 chatroomframe.java
字号:
package com.wczy.chatroom.clientui;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import com.wczy.chatroom.Protocal;
import com.wczy.chatroom.client.ChatRoomClient;
import com.wczy.chatroom.client.FileSaveOverListener;
import com.wczy.chatroom.client.FileSendOverListener;
import com.wczy.chatroom.client.OnlineListListener;
import com.wczy.chatroom.client.PrivateMessageListener;
import com.wczy.chatroom.client.PublicMessageListener;
import com.wczy.chatroom.client.SendFileAcceptListener;
import com.wczy.chatroom.client.SendFileRejectListener;
import com.wczy.chatroom.client.SendFileRequestListener;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JLabel;
public class ChatRoomFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JScrollPane jspHistory = null;
private JScrollPane jspOnlineUsers = null;
private JList lstUsers = null;
private JTextArea txtHistory = null;
private JTextField txtMessage = null;
private JButton btnSend = null;
private ChatRoomClient client;
private String nickName;
private HashMap<String, PrivateChatFrame> privateChatFrameMap = new HashMap<String, PrivateChatFrame>(); // @jve:decl-index=0:
private JLabel jLabel = null;
private JLabel jLabel1 = null;
public ChatRoomFrame(ChatRoomClient client, String nickName) {
super();
this.client = client;
this.nickName = nickName;
initialize();
addListeners();
}
private String formatMessage(String from,String message){
Calendar cal = Calendar.getInstance();
int h = cal.get(Calendar.HOUR_OF_DAY);
int m = cal.get(Calendar.MINUTE);
int s = cal.get(Calendar.SECOND);
return "["+((h>9)?"":"0")+h+":"
+((m>9)?"":"0")+ m+":"
+((s>9)?"":"0")+ s
+"]"
+" "
+from +" 说:\n "
+message;
}
private void addListeners(){
client.addOnlineListListener(new OnlineListListener() {
public void receive(List<String> nickNameList) {
DefaultListModel model = new DefaultListModel();
for (String nick : nickNameList) {
model.addElement(nick);
}
lstUsers.setModel(model);
}
});
client.addPublicMessageListener(new PublicMessageListener() {
public void receive(String from, String message) {
txtHistory.append(formatMessage(from, message) + "\n");
jspHistory.getVerticalScrollBar().setValue(
jspHistory.getVerticalScrollBar().getMaximum()
);
}
});
client.addPrivateMessageListener(new PrivateMessageListener() {
public void receive(String from, String message) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(from);
privateChatFrame.receiveMessage(from, message);
privateChatFrame.setVisible(true);
}
});
client
.addSendFileRequestListener(new SendFileRequestListener() {
public void receive(String from, String fileName) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(from);
privateChatFrame.setVisible(true);
int result = JOptionPane.showConfirmDialog(
privateChatFrame, from + "发送"
+ fileName + "给你,接受?", "警告",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
JFileChooser ch = new JFileChooser();
ch.setSelectedFile(new File(fileName));
ch.showSaveDialog(privateChatFrame);
client.receiveFile(from, ch
.getSelectedFile()
.getAbsolutePath());
} else {
client.sendFileReject(from);
}
}
});
client.addSendFileAcceptListener(new SendFileAcceptListener() {
public void receive(String nickName) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(nickName);
privateChatFrame.setVisible(true);
JOptionPane.showMessageDialog(privateChatFrame,
nickName + "同意接受文件");
}
});
client.addSendFileRejectListener(new SendFileRejectListener() {
public void receive(String nickName) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(nickName);
privateChatFrame.setVisible(true);
JOptionPane.showMessageDialog(privateChatFrame,
nickName + "拒绝接受文件");
}
});
client.addFileSaveOverListener(new FileSaveOverListener() {
public void saveOver(String nickName,String fileFullName) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(nickName);
privateChatFrame.setVisible(true);
JOptionPane.showMessageDialog(privateChatFrame,
fileFullName + "接收完毕");
}
});
client.addFileSendOverListener(new FileSendOverListener() {
public void sendOver(String nickName,String fileFullName) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(nickName);
privateChatFrame.setVisible(true);
JOptionPane.showMessageDialog(privateChatFrame,
fileFullName + "发送完毕");
}
});
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(470, 307);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
client.logout();
System.exit(0);
}
public void windowOpened(java.awt.event.WindowEvent e) {
ChatRoomFrame.this.setTitle("当前用户:"+nickName);
}
});
}
public PrivateChatFrame getPrivateChatFrame(String from) {
PrivateChatFrame privateChatFrame;
if (privateChatFrameMap.containsKey(from)) {
privateChatFrame = privateChatFrameMap.get(from);
} else {
privateChatFrame = new PrivateChatFrame(client, nickName, from);
privateChatFrameMap.put(from, privateChatFrame);
}
return privateChatFrame;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(278, 6, 79, 21));
jLabel1.setText(" 在线列表");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(9, 5, 81, 23));
jLabel.setText(" 聊天记录");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJspHistory(), null);
jContentPane.add(getJspOnlineUsers(), null);
jContentPane.add(getTxtMessage(), null);
jContentPane.add(getBtnSend(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
}
return jContentPane;
}
/**
* This method initializes jspHistory
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJspHistory() {
if (jspHistory == null) {
jspHistory = new JScrollPane();
jspHistory.setLocation(new Point(9, 29));
jspHistory.setSize(new Dimension(250, 200));
jspHistory.setViewportView(getTxtHistory());
}
return jspHistory;
}
/**
* This method initializes jspOnlineUsers
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJspOnlineUsers() {
if (jspOnlineUsers == null) {
jspOnlineUsers = new JScrollPane();
jspOnlineUsers.setBounds(new Rectangle(275, 31, 173, 195));
jspOnlineUsers.setViewportView(getLstUsers());
}
return jspOnlineUsers;
}
/**
* This method initializes lstUsers
*
* @return javax.swing.JList
*/
private JList getLstUsers() {
if (lstUsers == null) {
lstUsers = new JList();
lstUsers.setSize(new Dimension(170, 200));
lstUsers.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (e.getClickCount() > 1
&& !lstUsers.getSelectedValue().toString().equals(
nickName)) {
PrivateChatFrame privateChatFrame = getPrivateChatFrame(lstUsers
.getSelectedValue().toString());
privateChatFrame.setVisible(true);
}
}
});
}
return lstUsers;
}
/**
* This method initializes txtHistory
*
* @return javax.swing.JTextArea
*/
private JTextArea getTxtHistory() {
if (txtHistory == null) {
txtHistory = new JTextArea();
txtHistory.setEditable(false);
}
return txtHistory;
}
/**
* This method initializes txtMessage
*
* @return javax.swing.JTextField
*/
private JTextField getTxtMessage() {
if (txtMessage == null) {
txtMessage = new JTextField();
txtMessage.setLocation(new Point(9, 239));
txtMessage.setSize(new Dimension(369, 30));
}
return txtMessage;
}
/**
* This method initializes btnSend
*
* @return javax.swing.JButton
*/
private JButton getBtnSend() {
if (btnSend == null) {
btnSend = new JButton();
btnSend.setText("发送");
btnSend.setSize(new Dimension(60, 30));
btnSend.setLocation(new Point(388, 238));
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (txtMessage.getText().indexOf(Protocal.SEPARATOR) >= 0) {
JOptionPane.showMessageDialog(ChatRoomFrame.this,
"消息不得包含" + Protocal.SEPARATOR);
return;
}
if (txtMessage.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(ChatRoomFrame.this,
"消息不得为空");
return;
}
client.sendPublicMessage(txtMessage.getText());
txtMessage.setText("");
txtMessage.requestFocus();
}
});
}
return btnSend;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -