📄 chatface.java
字号:
package com.sato.client.chat;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.plaf.basic.BasicTreeUI.SelectionModelPropertyChangeHandler;
import com.sato.client.loginface.QQlogin;
import com.sato.pub.PackType;
import com.sato.pub.QQpack;
public class ChatFace extends JFrame{
private JPanel centerPanel;
private JPanel southPanel;
private JPanel leftPanel;
private JPanel rightPanel;
private JPanel leftUp;
private JPanel leftDown;
private JPanel rightUp;
private JPanel rightDown;
private JLabel chatRecord;
private JLabel showLabel;
private JLabel onlineLabel;
private JButton changePw;
private JButton recordBt;
private JButton sendBt;
private JButton closeBt;
private JTextArea recordArea;
private JTextArea showArea;
private JTextArea chatArea;
private JTextArea boardArea;
private Socket socket;
private ObjectInputStream ois;
private ObjectOutputStream oos;
private QQpack qqpack;
private JList onlineList;
private String id;
private String name;
private ChatListen chatListen;
/**
*
*/
public ChatFace(Socket socket,ObjectInputStream ois,ObjectOutputStream oos,QQpack qqpack) {
this.oos=oos;
this.ois=ois;
this.socket=socket;
this.qqpack=qqpack;
chatListen=new ChatListen(this,oos,ois);
id=qqpack.getFrom().getSid();
name=qqpack.getFrom().getSname();
MyClientReader mcr=new MyClientReader(socket,ois,oos,this);
mcr.start();
this.setSize(580, 480);
this.setLocationRelativeTo(null);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
windowClose();
}
});
// this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setTitle("QQ聊天窗口");
this.setResizable(false);
File filename = new File("./images/total/qqicon.gif");
Image jfimage = Toolkit.getDefaultToolkit().getImage(
filename.getAbsolutePath());
this.setIconImage(jfimage);
this.getContentPane().setLayout(new BorderLayout());
this.Centerp();
this.SouthPanel();
this.getContentPane().add(centerPanel,BorderLayout.CENTER);
this.getContentPane().add(southPanel,BorderLayout.SOUTH);
this.setVisible(true);
}
public JPanel Centerp(){
centerPanel=new JPanel();
centerPanel.setLayout(new BorderLayout());
this.LeftPanel();
this.RightPanel();
JSplitPane jstp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel );
jstp.setDividerLocation(380);
centerPanel.add(jstp,BorderLayout.CENTER);
return null;
}
public JPanel SouthPanel() {
chatRecord=new JLabel("聊天记录↓");
southPanel=new JPanel();
recordArea=new JTextArea(5,5);
recordArea.setEditable(false);
Box box=Box.createHorizontalBox();
box.add(chatRecord);
JScrollPane jscrol=new JScrollPane(recordArea);
southPanel.setLayout(new BorderLayout());
southPanel.add(jscrol,BorderLayout.CENTER);
southPanel.add(box,BorderLayout.NORTH);
southPanel.setVisible(false);
return null;
}
public JPanel LeftPanel() {
leftPanel=new JPanel();
leftPanel.setLayout(new BorderLayout());
this.LeftUp();
this.LeftDown();
JSplitPane jstp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, leftUp, leftDown);
jstp.setDividerLocation(280);
leftPanel.add(jstp,BorderLayout.CENTER);
return null;
}
public JPanel RightPanel() {
rightPanel=new JPanel();
rightPanel.setLayout(new BorderLayout());
this.RightUp();
this.RightDown();
JSplitPane jstp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, rightUp, rightDown);
jstp.setDividerLocation(160);
rightPanel.add(jstp,BorderLayout.CENTER);
return null;
}
public JPanel LeftUp() {
showLabel=new JLabel("群聊");
changePw=new JButton("修改密码");
Box box=Box.createHorizontalBox();
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(box,BorderLayout.CENTER);
panel.add(showLabel,BorderLayout.WEST);
panel.add(changePw,BorderLayout.EAST);
leftUp=new JPanel();
showArea=new JTextArea();
JScrollPane jscrol=new JScrollPane(showArea);
changePw.addActionListener(chatListen);
leftUp.setLayout(new BorderLayout());
leftUp.add(jscrol,BorderLayout.CENTER);
leftUp.add(panel,BorderLayout.NORTH);
return null;
}
public JPanel LeftDown() {
recordBt=new JButton("聊天记录");
sendBt=new JButton("发送");
closeBt=new JButton("关闭");
Box box=Box.createHorizontalBox();
box.add(recordBt,Box.LEFT_ALIGNMENT);
box.add(Box.createHorizontalStrut(160));
box.add(sendBt,Box.CENTER_ALIGNMENT);
box.add(Box.createHorizontalStrut(15));
box.add(closeBt,Box.RIGHT_ALIGNMENT);
leftDown=new JPanel();
chatArea=new JTextArea(5,5);
recordBt.addActionListener(chatListen);
sendBt.addActionListener(chatListen);
closeBt.addActionListener(chatListen);
JScrollPane jscrol=new JScrollPane(chatArea);
leftDown.setLayout(new BorderLayout());
leftDown.add(jscrol,BorderLayout.CENTER);
leftDown.add(box,BorderLayout.SOUTH);
return null;
}
public JPanel RightUp() {
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
rightUp=new JPanel();
rightUp.setLayout(new BorderLayout());
boardArea=new JTextArea();
boardArea.setEditable(false);
JScrollPane jscrol=new JScrollPane(boardArea);
p.add(jscrol,BorderLayout.CENTER);
JTabbedPane jtp=new JTabbedPane();
jtp.addTab("公告信息",p);
rightUp.add(jtp,BorderLayout.CENTER);
return null;
}
public JPanel RightDown() {
rightDown=new JPanel();
rightDown.setLayout(new BorderLayout());
onlineList=new JList();
JListlisten jlisten=new JListlisten(this);
onlineList.addListSelectionListener(jlisten);
onlineList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane jscrol=new JScrollPane(onlineList);
onlineLabel=new JLabel("在线用户↓");
rightDown.add(onlineLabel, BorderLayout.NORTH);
rightDown.add(jscrol, BorderLayout.CENTER);
return null;
}
public void windowClose(){
QQpack qqpack=new QQpack();
qqpack.setContent(id);
qqpack.setType(PackType.offline);
try {
oos.writeObject(qqpack);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
public JPanel getCenterPanel() {
return centerPanel;
}
public void setCenterPanel(JPanel centerPanel) {
this.centerPanel = centerPanel;
}
public JPanel getSouthPanel() {
return southPanel;
}
public void setSouthPanel(JPanel southPanel) {
this.southPanel = southPanel;
}
public JPanel getLeftPanel() {
return leftPanel;
}
public void setLeftPanel(JPanel leftPanel) {
this.leftPanel = leftPanel;
}
public JPanel getRightPanel() {
return rightPanel;
}
public void setRightPanel(JPanel rightPanel) {
this.rightPanel = rightPanel;
}
public JPanel getLeftUp() {
return leftUp;
}
public void setLeftUp(JPanel leftUp) {
this.leftUp = leftUp;
}
public JPanel getLeftDown() {
return leftDown;
}
public void setLeftDown(JPanel leftDown) {
this.leftDown = leftDown;
}
public JPanel getRightUp() {
return rightUp;
}
public void setRightUp(JPanel rightUp) {
this.rightUp = rightUp;
}
public JPanel getRightDown() {
return rightDown;
}
public void setRightDown(JPanel rightDown) {
this.rightDown = rightDown;
}
public JLabel getChatRecord() {
return chatRecord;
}
public void setChatRecord(JLabel chatRecord) {
this.chatRecord = chatRecord;
}
public JLabel getShowLabel() {
return showLabel;
}
public void setShowLabel(JLabel showLabel) {
this.showLabel = showLabel;
}
public JLabel getOnlineLabel() {
return onlineLabel;
}
public void setOnlineLabel(JLabel onlineLabel) {
this.onlineLabel = onlineLabel;
}
public JButton getChangePw() {
return changePw;
}
public void setChangePw(JButton changePw) {
this.changePw = changePw;
}
public JButton getRecordBt() {
return recordBt;
}
public void setRecordBt(JButton recordBt) {
this.recordBt = recordBt;
}
public JButton getSendBt() {
return sendBt;
}
public void setSendBt(JButton sendBt) {
this.sendBt = sendBt;
}
public JButton getCloseBt() {
return closeBt;
}
public void setCloseBt(JButton closeBt) {
this.closeBt = closeBt;
}
public JTextArea getRecordArea() {
return recordArea;
}
public void setRecordArea(JTextArea recordArea) {
this.recordArea = recordArea;
}
public JTextArea getShowArea() {
return showArea;
}
public void setShowArea(JTextArea showArea) {
this.showArea = showArea;
}
public JTextArea getChatArea() {
return chatArea;
}
public void setChatArea(JTextArea chatArea) {
this.chatArea = chatArea;
}
public JTextArea getBoardArea() {
return boardArea;
}
public void setBoardArea(JTextArea boardArea) {
this.boardArea = boardArea;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public ObjectInputStream getOis() {
return ois;
}
public void setOis(ObjectInputStream ois) {
this.ois = ois;
}
public ObjectOutputStream getOos() {
return oos;
}
public void setOos(ObjectOutputStream oos) {
this.oos = oos;
}
public QQpack getQqpack() {
return qqpack;
}
public void setQqpack(QQpack qqpack) {
this.qqpack = qqpack;
}
public JList getOnlineList() {
return onlineList;
}
public void setOnlineList(JList onlineList) {
this.onlineList = onlineList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -