📄 personalchat.java
字号:
package com.gamvan.club.users.im.client.personalchat;
import com.gamvan.club.users.im.client.IMPanelClient;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;
//import javax.sound.sampled.AudioFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.plaf.metal.MetalButtonUI;
//import org.jsresources.apps.am.audio.AMAudioFormat;
import org.jsresources.apps.am.audio.AudioCapture;
import org.jsresources.apps.am.audio.AudioPlayStream;
import com.gamvan.club.users.im.client.personalchat.mylist.IconListItem;
import com.gamvan.club.users.im.client.personalchat.mylist.MyList;
import com.gamvan.club.users.im.tools.MyTextPane;
import com.gamvan.club.users.im.tools.NwFontChooserS;
public class PersonalChat extends JFrame {
/**
* 私聊程序
*/
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private static PersonalChat thisClass = null;
private JTabbedPane jTabbedPane = null;
private JPanel chatPanel = null;
private JPanel viewPanel = null;
private JMenuBar jJMenuBar = null;
private JMenu sendMenu = null;
private JMenuItem sendMeg = null;
private JMenu set = null;
private JMenuItem fontSet = null;
private JMenuItem colorSet = null;
//private ObjectInputStream dataIn = null;
private JScrollPane RecevScrollPane = null;
private JScrollPane sendScrollPane = null;
public MyTextPane recevTextPane = null;
private MyTextPane sendTextPane = null;
private JPanel otherPanelR = null;
public MyTextPane chatNotesTextPane = null;
private JScrollPane chatMegScrollPane = null;
private JMenuItem saveChatNotes = null;
private MyList jList = null,jList2;
private JScrollPane jScrollPane1 = null;
private JMenuItem clearItem = null;
private Socket clientSocket = null; // @jve:decl-index=0:
private ObjectOutputStream dataOut = null; // @jve:decl-index=0:
private Date date = null;
private Color color = null;
private JComboBox actionList,userOnlineList;
private Font font = null;
private JFileChooser saveChooser = null;
private String saveNotes;
private JMenuItem exitItem = null;
private JPanel phonePanel = null;
private JButton exit = null;
private JButton sendButton = null;
private JScrollPane msgScrollPane = null;
private MyTextPane voiceTextPane = null;
public AudioCapture PhoneMIC = null;
public AudioPlayStream PhoneSPK = null;
//private AudioFormat format = null;
/**
* This method initializes jTabbedPane
*
* @return javax.swing.JTabbedPane
*/
private JTabbedPane getJTabbedPane() {
if (jTabbedPane == null) {
jTabbedPane = new JTabbedPane();
jTabbedPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
jTabbedPane.addTab("聊天", null, getChatPanel(), null);
jTabbedPane.addTab("聊天记录", null, getViewPanel(), null);
jTabbedPane.addTab("语音", null, getPhonePanel(), null);
}
return jTabbedPane;
}
/**
* This method initializes chatPanel
*
* @return javax.swing.JPanel
*/
private JPanel getChatPanel() {
if (chatPanel == null) {
chatPanel = new JPanel();
chatPanel.setLayout(new BorderLayout());
chatPanel.add(getRecevScrollPane(), BorderLayout.CENTER);
chatPanel.add(getSendScrollPane(), BorderLayout.SOUTH);
chatPanel.add(getOtherPanelR(), BorderLayout.EAST);
}
return chatPanel;
}
/**
* This method initializes viewPanel
*
* @return javax.swing.JPanel
*/
private JPanel getViewPanel() {
if (viewPanel == null) {
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.weighty = 1.0;
gridBagConstraints1.weightx = 1.0;
viewPanel = new JPanel();
viewPanel.setLayout(new GridBagLayout());
viewPanel.add(getChatMegScrollPane(), gridBagConstraints1);
}
return viewPanel;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getSendMenu());
jJMenuBar.add(getSet());
}
return jJMenuBar;
}
/**
* This method initializes sendMenu
*
* @return javax.swing.JMenu
*/
private JMenu getSendMenu() {
if (sendMenu == null) {
sendMenu = new JMenu("发送");
sendMenu.add(getSendMeg());
sendMenu.add(getSaveChatNotes());
sendMenu.add(getClearItem());
sendMenu.add(getExitItem());
}
return sendMenu;
}
/**
* This method initializes sendMeg
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getSendMeg() {
if (sendMeg == null) {
sendMeg = new JMenuItem("发送信息");
sendMeg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.ALT_MASK,true));
sendMeg.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
sendMessage();
sendTextPane.setText("");
}
});
}
return sendMeg;
}
private void sendMessage(){
//得到信息发送对象
String toSomebody = userOnlineList.getSelectedItem().toString();
String action = actionList.getSelectedItem().toString();
String message = sendTextPane.getText();
if(clientSocket.isClosed()){
return;
}
if(message.equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"抱歉,不能发送空消息",
"系统警告",JOptionPane.WARNING_MESSAGE);
return;
}else{
try{
dataOut.writeObject("私聊信息");//发送聊天信息标识
dataOut.flush();
dataOut.writeObject(toSomebody);//发送聊天信息对象
dataOut.flush();
dataOut.writeObject(getDate().toString());//发送信息发送时间
dataOut.flush();
dataOut.writeObject("<personal>" + "\n\n");//发送聊天信息状态
dataOut.flush();
dataOut.writeObject(action);//发送聊天信息表情
dataOut.flush();
dataOut.writeObject(message);//发送聊天信息内容
dataOut.flush();
if(color != null){
dataOut.writeObject(color);
dataOut.flush();
}else{
dataOut.writeObject(Color.blue);
dataOut.flush();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public void sendObjects(Socket clientSocket,ObjectInputStream dataIn,
ObjectOutputStream dataOut,
JComboBox userOnlineList,JCheckBox statusCheckBox,JComboBox actionList,
MyList jList){
this.clientSocket = clientSocket;
this.dataOut = dataOut;
this.userOnlineList = userOnlineList;
this.actionList = actionList;
this.jList2 = jList;
//this.dataIn = dataIn;
}
private Date getDate(){
if(date == null){
date = new Date();
}
return date;
}
/**
* This method initializes set
*
* @return javax.swing.JMenu
*/
private JMenu getSet() {
if (set == null) {
set = new JMenu();
set.setText("设置");
set.add(getFontSet());
set.add(getColorSet());
}
return set;
}
/**
* This method initializes fontSet
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getFontSet() {
if (fontSet == null) {
fontSet = new JMenuItem("字体设置");
fontSet.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.ALT_MASK,true));
fontSet.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
font = NwFontChooserS.showDialog(
thisClass,"字体设置",font);
recevTextPane.setFont(font);
}
});
}
return fontSet;
}
/**
* This method initializes colorSet
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getColorSet() {
if (colorSet == null) {
colorSet = new JMenuItem("颜色设置");
colorSet.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.ALT_MASK,true));
colorSet.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
color = JColorChooser.showDialog(
sendTextPane, "前景色", color);
sendTextPane.setForeground(color);
}
});
}
return colorSet;
}
/**
* This method initializes RecevScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getRecevScrollPane() {
if (RecevScrollPane == null) {
RecevScrollPane = new JScrollPane();
RecevScrollPane.setViewportView(getRecevTextPane());
}
return RecevScrollPane;
}
/**
* This method initializes sendScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getSendScrollPane() {
if (sendScrollPane == null) {
sendScrollPane = new JScrollPane();
sendScrollPane.setViewportView(getSendTextPane());
}
return sendScrollPane;
}
/**
* This method initializes recevTextPane
*
* @return javax.swing.JTextPane
*/
private MyTextPane getRecevTextPane() {
if (recevTextPane == null) {
recevTextPane = new MyTextPane();
}
return recevTextPane;
}
/**
* This method initializes sendTextPane
*
* @return javax.swing.JTextPane
*/
private MyTextPane getSendTextPane() {
if (sendTextPane == null) {
sendTextPane = new MyTextPane();
sendTextPane.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if(e.getKeyChar() == (char)KeyEvent.VK_ENTER){
if(clientSocket == null||clientSocket.isClosed()){
return;
}else{
sendMessage();
sendTextPane.setText("");
}
}
}
});
}
return sendTextPane;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -