📄 qqchatframe.java
字号:
//聊天窗体
package myQQ;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
class QQChatFrame extends JFrame implements ActionListener
{
QQClient client = null;
String sendId; //发送方id
String sendPn; //发送方昵称
String readId; //接收方id
String readPn; //接收方昵称
String readHead; //接收方头像图片名
Calendar time; //时间
int x,y,w,h;
//标题面板
QQPanel title = new QQPanel(0,"chat/p0");
//聊天面板
QQEditorPane chatMain = new QQEditorPane();
QQTextPane chatIn = new QQTextPane();
//类型面板----------开始
QQPanel chatType = new QQPanel(0,"chat/p0");
QQButton[] jbType = new QQButton[5];
ImageIcon[] icTypeP = new ImageIcon[5];
String[] sType = {"文字类型","选择表情","选择场景","文件传送","播放音乐"};
//字体选择面板
QQFontChooser font = new QQFontChooser(chatIn);
//类型面板----------结束
//发送面板----------开始
QQPanel chatSend = new QQPanel(0,"chat/p0");
QQButton[] jbSend = new QQButton[4];
String[] sSend = {"聊天记录","关闭","发送",""};
//发送面板----------结束
//附加
Clock chatFile; //放置时钟
Video chatMusic = new Video(); //播放音乐
QQPanel tem = new QQPanel(0,"chat/p0"); //
Border b = new EtchedBorder();
JPopupMenu pMenu = new JPopupMenu();
JRadioButtonMenuItem m1 = new JRadioButtonMenuItem("按Enter键发送...");
JRadioButtonMenuItem m2 = new JRadioButtonMenuItem("按Cril+Enter键发送...");
boolean flagSend = false; //控制发送快捷键
QQChatFrame(QQClient client,String Sid,String Spn,User u)
{
//初始消息各属性
this.client = client;
this.sendId = Sid;
this.sendPn = Spn;
this.readHead = u.readHead;
this.readId = u.readId;
this.readPn = u.readPn;
this.setIconImage(Adom.icon); //设置图标
this.setSize(480,460);
Point p = new Adom().setCenter(this.getSize());
this.setLocation(p);
Container c = this.getContentPane();
c.setLayout(null);
w = this.getWidth()-5;
h = this.getHeight()-30;
this.setTitle("与"+this.readPn+"聊天ing");
//标题面板
title.setBounds(0,0,w,(int)(h*0.1));
title.setLayout(null);
User uT = new User(u.readHead,u.readPn,u.readId);
uT.setBounds(10,2,200,38);
title.add(uT);
c.add(title);
//聊天主面板
chatMain.setBounds(0,(int)(h*0.1),(int)(w*0.7),(int)(h*0.55));
c.add(chatMain);
chatMain.unWrite(true); //不可编辑
//类型面板
chatType.setBounds(0,(int)(h*0.65),(int)(w*0.7),(int)(h*0.06));
//字体面板暂时隐藏
font.setBounds(0,(int)(h*0.58),(int)(w*0.7),(int)(h*0.07));
this.buildChatTypePane();
c.add(chatType);
//聊天传入面板
chatIn.setBounds(0,(int)(h*0.71),(int)(w*0.7),(int)(h*0.2));
c.add(chatIn);
chatIn.jtp.addKeyListener(new KE());
//发送按钮面板
chatSend.setBounds(0,(int)(h*0.91),(int)(w*0.7),(int)(h*0.09));
this.buildChatSendPane();
c.add(chatSend);
//文件上传面板
chatFile = new Clock((int)(w*0.3),(int)(h*0.35));
chatFile.setBounds((int)(w*0.7),(int)(h*0.1),(int)(w*0.3),(int)(h*0.35));
c.add(chatFile);
QQPanel temsub = new QQPanel("QQ/skin/chat/tem.gif");
tem.setLayout(null);
temsub.setBounds(20,20,100,40);
tem.setBounds((int)(w*0.7),(int)(h*0.45),(int)(w*0.3),(int)(h*0.2));
c.add(tem);
tem.add(temsub);
//音乐面板
chatMusic.setBounds((int)(w*0.7),(int)(h*0.65),(int)(w*0.3),(int)(h*0.35));
c.add(chatMusic);
//-----浮动菜单
pMenu.add(m1);
pMenu.add(m2);
ButtonGroup bg = new ButtonGroup();
bg.add(m1); bg.add(m2);
m2.setSelected(true);
m1.addActionListener(this);
m2.addActionListener(this);
this.setResizable(false);
this.setVisible(true);
}
public void buildChatTypePane()
{ //类型面板构架
chatType.setLayout(null);
for (int i = 0; i<jbType.length; i++)
{
icTypeP[i] = new ImageIcon("QQ/skin/chat/t"+i+".png");
jbType[i] = new QQButton(icTypeP[i],Color.BLUE,sType[i]);
jbType[i].addActionListener(this);
jbType[i].setBounds(10+i*30,2,20,20);
chatType.add(jbType[i]);
}
}
public void buildChatSendPane()
{ //发送面板构架
chatSend.setLayout(null);
for (int i = 0; i<jbSend.length; i++)
{
if(i<2)
jbSend[i] = new QQButton("QQ/skin/default/s"+i,sSend[i]);
else
jbSend[i] = new QQButton("QQ/skin/default/s"+(i-1),sSend[i]);
if(i<3)
jbSend[i].addActionListener(this);
else
jbSend[i].addMouseListener(new ME());
chatSend.add(jbSend[i]);
}
jbSend[0].setBounds(10,8,71,22);
jbSend[1].setBounds(195,8,50,22);
jbSend[2].setBounds(250,8,50,22);
jbSend[3].setBounds(305,8,10,22);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(jbType[0]))
{ //字体选择按钮
if(!jbType[0].click)
this.showFontPane(true);
else
this.showFontPane(false);
}
if(e.getSource().equals(jbType[1]))
{ //表情选择按钮
Point p = jbType[1].getLocationOnScreen();
new QQFace(p,this).toFront();
}
if(e.getSource().equals(jbSend[0]))
{ //聊天记录
}
if(e.getSource().equals(jbSend[1]))
{ //关闭窗体
//----------连接处理---------
this.dispose();
}
if(e.getSource().equals(jbSend[2]))
{ //发送
this.sendChatInfo();
}
if(e.getSource().equals(m1))
{ //
flagSend = true;
}
if(e.getSource().equals(m2))
{ //
flagSend = false;
}
}
class ME extends MouseAdapter
{
public void mouseReleased(MouseEvent e)
{ //浮动菜单
pMenu.show(e.getComponent(),e.getX(),e.getY());
chatIn.jtp.requestFocus(true);
}
}
class KE extends KeyAdapter
{ //键盘事件----发送快捷键
public void keyReleased(KeyEvent k)
{
if(flagSend)
{
if(k.getKeyCode()==k.VK_ENTER)
sendChatInfo();
}
else
{
if(k.getKeyCode()==k.VK_ENTER&&k.isControlDown())
sendChatInfo();
}
}
}
public void showFontPane(boolean show)
{ //字体类型面板的显示或隐藏
if(show)
{ //显示
chatMain.setBounds(0,(int)(h*0.1),(int)(w*0.7),(int)(h*0.48));
this.getContentPane().add(font);
}
else
{ //隐藏
chatMain.setBounds(0,(int)(h*0.1),(int)(w*0.7),(int)(h*0.55));
this.getContentPane().remove(font);
}
}
public void sendChatInfo()
{ //发送聊天信息
if(chatIn.isEmpty())
{ //验证
JOptionPane.showMessageDialog(null,"发送内容不能为空!","提示",0,Adom.icOP);
}
else
{ //取得内容和时间
String s = chatIn.getContent();
time = Calendar.getInstance();
//构建消息对象
MessType mt = new MessType(this.sendId,this.readId,s,time,10);
mt.SendName = this.sendPn;
//显示消息
chatMain.unWrite(false);
chatMain.writeSaying(this.sendPn,time,Color.BLUE);
chatMain.setContent(s);
chatMain.unWrite(true);
//发送
client.sendMess(mt);
chatIn.setEmpty();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -