📄 chatroomform.java
字号:
package qianqian.p2pchat.gui;
import qianqian.p2pchat.control.*;
public class ChatRoomForm extends RoomForm {
private static final long serialVersionUID = 1L;
private javax.swing.JScrollPane msgScroller;
private javax.swing.JScrollPane sendScroller;
private javax.swing.JPanel imgPanel;
private javax.swing.JScrollPane frndListScroller;
private javax.swing.JTextArea msgArea;
private javax.swing.JSplitPane panSplit;
private javax.swing.JSplitPane textSplit;
private javax.swing.JSplitPane frndSplit;
private javax.swing.JToolBar toolbar;
private javax.swing.JTextArea sendArea;
private javax.swing.JMenuItem sendItem;
private javax.swing.JMenuItem clrItem;
private javax.swing.JMenuItem helpItem;
private javax.swing.JLabel lbImg;
private javax.swing.border.MatteBorder border;
private java.awt.Color bgColor;
private FriendTree nickList;
private static ChatRoomForm chatRoomForm = null;
private ChatRoomForm() {
initComponents();
addListener();
setBounds(305, 150, 414, 400);
requestFocus();
}
public static synchronized ChatRoomForm getInstance() {
if(chatRoomForm == null)
chatRoomForm = new ChatRoomForm();
return chatRoomForm;
}
public void dispose() {
chatRoomForm = null;
super.dispose();
}
private void initComponents() {
panSplit = new javax.swing.JSplitPane();
textSplit = new javax.swing.JSplitPane(
javax.swing.JSplitPane.VERTICAL_SPLIT);
frndSplit = new javax.swing.JSplitPane(
javax.swing.JSplitPane.VERTICAL_SPLIT);
frndListScroller = new javax.swing.JScrollPane();
msgScroller = new javax.swing.JScrollPane();
sendScroller = new javax.swing.JScrollPane();
imgPanel = new javax.swing.JPanel();
msgArea = new javax.swing.JTextArea();
sendArea = new javax.swing.JTextArea();
toolbar = new javax.swing.JToolBar();
sendItem = new javax.swing.JMenuItem("发 送");
clrItem = new javax.swing.JMenuItem("清 屏");
helpItem = new javax.swing.JMenuItem("帮 助");
if (Controller.getInstance().getMe().getGender() == '男') {
setIconImage(new javax.swing.ImageIcon("icons/QG.gif")
.getImage());
lbImg = new javax.swing.JLabel(new javax.swing.ImageIcon(
"icons/GG.gif"));
} else {
setIconImage(new javax.swing.ImageIcon("icons/QM.gif")
.getImage());
lbImg = new javax.swing.JLabel(new javax.swing.ImageIcon(
"icons/QQ.gif"));
}
border = new javax.swing.border.MatteBorder(new java.awt.Insets(1, 1,
1, 1), new java.awt.Color(195, 225, 255));
bgColor = new java.awt.Color(235, 245, 255);
nickList = new FriendTree();
nickList.setBorder(border);
frndListScroller.setAutoscrolls(true);
frndListScroller.setViewportView(nickList);
imgPanel.setBorder(border);
imgPanel.setBackground(java.awt.Color.WHITE);
imgPanel.setLayout(new java.awt.BorderLayout());
imgPanel.add(lbImg, java.awt.BorderLayout.CENTER);
msgArea.setLineWrap(true);
msgArea.setBorder(border);
msgArea.setEditable(false);
msgScroller.setAutoscrolls(true);
msgScroller.setViewportView(msgArea);
sendArea.setLineWrap(true);
sendArea.setBorder(border);
sendScroller.setAutoscrolls(true);
sendScroller.setViewportView(sendArea);
frndSplit.setTopComponent(imgPanel);
frndSplit.setBottomComponent(frndListScroller);
frndSplit.setDividerLocation(110);
frndSplit.setDividerSize(2);
textSplit.setTopComponent(msgScroller);
textSplit.setBottomComponent(sendScroller);
textSplit.setDividerLocation(235);
textSplit.setDividerSize(2);
panSplit.setRightComponent(frndSplit);
panSplit.setLeftComponent(textSplit);
panSplit.setDividerLocation(280);
panSplit.setDividerSize(2);
sendItem.setBackground(bgColor);
toolbar.add(sendItem);
clrItem.setBackground(bgColor);
toolbar.add(clrItem);
helpItem.setBackground(bgColor);
toolbar.add(helpItem);
toolbar.setBorder(border);
getContentPane().setLayout(new java.awt.BorderLayout());
getContentPane().add(panSplit, java.awt.BorderLayout.CENTER);
getContentPane().add(toolbar, java.awt.BorderLayout.SOUTH);
pack();
}
private void addListener() {
// 关闭窗口
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
// 消息发送事件响应
sendItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendMsg();
}
});
// 清屏事件响应
clrItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clrScreen();
}
});
// 帮助事件响应
helpItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showHelp();
}
});
sendItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_ENTER,
java.awt.event.InputEvent.CTRL_MASK, true));
clrItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_DELETE,
java.awt.event.InputEvent.CTRL_MASK, true));
}
private void clrScreen() {
msgArea.setText("");
}
public void updateList(java.util.LinkedList<Peer> iPeers, boolean arg) {
if(iPeers != null)
nickList.update(iPeers, arg);
}
private void sendMsg() {
if (sendArea.getText().isEmpty()) {
javax.swing.JOptionPane.showMessageDialog(msgArea, "发送内容为空!",
"提示", javax.swing.JOptionPane.INFORMATION_MESSAGE);
return;
}
Controller.getInstance().sendRoomMessage(sendArea.getText());
sendArea.setText("");
}
private void showHelp() {
javax.swing.JOptionPane.showMessageDialog(msgArea,
"1.双击好友头像与其进行单独聊天!\n" +
"2.单独聊天窗口输入/help查看命令行!", "提示",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
public void addRecvLine(String iMsg) {
msgArea.setText(msgArea.getText() + iMsg + "\n");
msgArea.scrollRectToVisible(new java.awt.Rectangle(0, msgArea
.getHeight() - 20, 1, 1));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -