📄 leftpane.java
字号:
package client.chat.chatpane;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
import utils.InfoPacket;
import utils.StaticUtils;
/**
* 聊天窗口界面的左边面板,用于创建显示聊天信息的面板和发送信息的面板
* @author 洪景泉
*
*/
public class LeftPane extends JPanel{
private static final long serialVersionUID = -2139620546730485424L;
private ChatEvent ce=null;
private InputStream is=null;
private OutputStream os=null;
private ChatDialog cd=null;
private SimpleDateFormat sdf=null;
private JPanel pane1=null;
private JPanel pane2=null;
//pane1面板部分
private JPanel toUserPane=null;
private JLabel toLabel=null;
private JLabel iconLabel=null;
private JLabel nameLabel=null;
private JTextPane showText=null;
private JScrollPane showPane=null;
// 发送面板pane2部分
private JButton fontBtn = null;
private JButton faceBtn = null;
private JButton imgBtn = null;
private JButton fileBtn = null;
private JTextPane sendText = null;
private JButton closeBtn=null;
private JButton sendBtn=null;
private JButton chatLogBtn=null;
private JPanel btnPane=null;
private JPanel topPane=null;
/**
* 构造函数
* @param chatFrame 主窗体对象
* @param is 输入流
* @param os 输出流
*/
public LeftPane(ChatFrame chatFrame,InputStream is,OutputStream os) {
this.is=is;
this.os=os;
toLabel=new JLabel("与 ");
iconLabel=new JLabel();//用户头像
nameLabel=new JLabel();//用户姓名
Box vBox=Box.createVerticalBox();
Box hBox=Box.createHorizontalBox();
hBox.add(Box.createHorizontalStrut(10));
hBox.add(toLabel);
hBox.add(iconLabel);
hBox.add(nameLabel);
hBox.add(Box.createHorizontalStrut(230));
vBox.add(hBox);
toUserPane=new JPanel();
toUserPane.add(vBox);
showText=new JTextPane();
showText.setEditable(false);
showPane=new JScrollPane(showText);
pane1=new JPanel();
pane1.setLayout(new BorderLayout());
pane1.add(toUserPane,BorderLayout.NORTH);
pane1.add(showPane,BorderLayout.CENTER);
//按钮面板部分
fontBtn = new JButton();
fontBtn.setBorder(null);
fontBtn.setIcon(new ImageIcon("./image/total/setfont.gif"));
faceBtn=new JButton();
faceBtn.setBorder(null);
faceBtn.setIcon(new ImageIcon("./image/total/setface.gif"));
imgBtn=new JButton();
imgBtn.setBorder(null);
imgBtn.setIcon(new ImageIcon("./image/total/sendimg.gif"));
fileBtn=new JButton();
fileBtn.setActionCommand("fileBtn");
fileBtn.setBorder(null);
fileBtn.setIcon(new ImageIcon("./image/total/sendfile.gif"));
topPane = new JPanel();
FlowLayout flowLay = new FlowLayout();
flowLay.setAlignment(FlowLayout.LEFT);
topPane.setLayout(flowLay);
Box vBox2 = Box.createVerticalBox();
Box hBox2 = Box.createHorizontalBox();
hBox2.add(fontBtn);
hBox2.add(faceBtn);
hBox2.add(imgBtn);
hBox2.add(fileBtn);
vBox2.add(hBox2);
topPane.add(vBox2);
sendText = new JTextPane();
JScrollPane jspan = new JScrollPane(sendText);
chatLogBtn=new JButton("聊天记录");
closeBtn=new JButton("关闭");
sendBtn=new JButton("发送");
sendBtn.setToolTipText("按Ctrl+Enter键发送信息");
btnPane = new JPanel();
btnPane.setLayout(new BorderLayout());
Box vBox3 = Box.createVerticalBox();
Box hBox3 = Box.createHorizontalBox();
hBox3.add(Box.createHorizontalStrut(5));
hBox3.add(chatLogBtn);
hBox3.add(Box.createHorizontalStrut(80));
hBox3.add(closeBtn);
hBox3.add(Box.createHorizontalStrut(15));
hBox3.add(sendBtn);
vBox3.add(Box.createVerticalStrut(5));
vBox3.add(hBox3);
vBox3.add(Box.createVerticalStrut(5));
btnPane.add(vBox3);
pane2=new JPanel();
pane2.setLayout(new BorderLayout());
pane2.add(topPane,BorderLayout.NORTH);
pane2.add(jspan,BorderLayout.CENTER);
pane2.add(btnPane,BorderLayout.SOUTH);
JSplitPane sppnTotle = new JSplitPane(JSplitPane.VERTICAL_SPLIT,pane1,pane2);
sppnTotle.setDividerLocation(300);
this.setLayout(new BorderLayout());
this.add(sppnTotle,BorderLayout.CENTER);
//鼠标事件
closeBtn.setActionCommand("close");
sendBtn.setActionCommand("send");
chatLogBtn.setActionCommand("chatLog");
fileBtn.setActionCommand("fileBtn");
cd=new ChatDialog(chatFrame);
ce=new ChatEvent(chatFrame,is,os,cd);
closeBtn.addActionListener(ce);
sendBtn.addActionListener(ce);
chatLogBtn.addActionListener(ce);
fileBtn.addActionListener(ce);
//键盘事件
sendText.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {
if(e.isControlDown()&&e.getKeyCode()==e.VK_ENTER){
//System.out.println("123456");
// 发送
// String sendStr = sendText.getText();
// HashMap chatInfo=new HashMap();
// chatInfo.put("Info",sendStr);
String chatInfo=sendText.getText();
// 发送
String userID=(String)StaticUtils.currentUser.get("userID");
String currentuser=(String)StaticUtils.currentUser.get("userName");
String to=(String)StaticUtils.currentUser.get("to");
InfoPacket infoPacket = new InfoPacket(InfoPacket.CHAT,userID+":"+currentuser,to,chatInfo);
try {
LeftPane.this.os.write(infoPacket.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
}
// 显示
StyledDocument showDoc = showText.getStyledDocument();
showText.setEditable(true);
try {
sdf = new SimpleDateFormat ("HH:mm:ss");
showDoc.insertString(showDoc.getLength(),currentuser+" "+sdf.format(new Date())+"\n",null);
showDoc.insertString(showDoc.getLength(), chatInfo, null);
showDoc.insertString(showDoc.getLength(), "\n", null);
} catch (Exception ex) {
ex.printStackTrace();
}
// 清空输入框
sendText.setText("");
showText.setEditable(false);
// 把当前用户发送的聊天记录放在追加到聊天记录对话框里面
cd.getChatHistory().append(" "+currentuser+" "+sdf.format(new Date())+"\n");
cd.getChatHistory().append(" "+chatInfo+"\n");
}
}
public void keyReleased(KeyEvent e) {
// TODO 自动生成方法存根
}
public void keyTyped(KeyEvent e) {
// TODO 自动生成方法存根
}
});
}
public JLabel getIconLabel() {
return iconLabel;
}
public void setIconLabel(JLabel iconLabel) {
this.iconLabel = iconLabel;
}
public JLabel getNameLabel() {
return nameLabel;
}
public void setNameLabel(JLabel nameLabel) {
this.nameLabel = nameLabel;
}
public JTextPane getSendText() {
return sendText;
}
public void setSendText(JTextPane sendText) {
this.sendText = sendText;
}
public JTextPane getShowText() {
return showText;
}
public void setShowText(JTextPane showText) {
this.showText = showText;
}
public ChatDialog getCd() {
return cd;
}
public void setCd(ChatDialog cd) {
this.cd = cd;
}
public ChatEvent getCe() {
return ce;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -