impanelclient.java
来自「注:语音聊天部分还未完成」· Java 代码 · 共 1,254 行 · 第 1/3 页
JAVA
1,254 行
package com.gamvan.club.users.im.client;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Cursor;import java.awt.Dimension;import java.awt.Font;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.io.File;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.MalformedURLException;import java.net.Socket;import java.util.Date;import java.util.Locale;import java.util.Random;import javax.swing.BoxLayout;import javax.swing.ImageIcon;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.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextField;import javax.swing.KeyStroke;import javax.swing.SwingUtilities;import com.gamvan.club.users.im.client.personalchat.mylist.MyList;import com.gamvan.club.users.im.tools.ExampleFileFilter;import com.gamvan.club.users.im.tools.MyTextPane;import com.gamvan.club.users.im.tools.NwFontChooserS;import com.gamvan.club.users.im.tools.formatdatetime.FormatDateTime;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;public class IMPanelClient extends JFrame{ /** * 会员客户端的类 * 幽梦还乡 */ private static final long serialVersionUID = 1L; private JSplitPane mainSplit = null; private Date date = null; private JPanel jContentPane = null; private JPanel jPanel = null; private JPanel otherPane = null; private JButton login = null; private JButton sendMeg = null; private JButton logout = null; private JButton exit = null; private JButton hideLogin = null; private JTextField sendMegTextPane = null; private JScrollPane sendMegScrollPane = null; private JScrollPane jListScrollPane = null; private JScrollPane receiveMegScrollPane = null; private MyTextPane receiveMeg = null; private MyList jList = null; private JCheckBox statusCheckBox = null; private JTextField userName = null; private JComboBox userOnlineList = null; private JComboBox actionList = null; private JPopupMenu popMenu = null; private JPopupMenu popMenu1 = null; // @jve:decl-index=0: private JMenuItem clearText = null;//清空文本 private JMenuItem setsedFont = null; private JMenuItem fontSet = null; private Socket clientSocket = null; // @jve:decl-index=0: public ObjectInputStream dataIn = null; public ObjectOutputStream dataOut = null; // @jve:decl-index=0: private ClientReceiveThread recevThread = null; // @jve:decl-index=0: private Random hideLoginCount = null; // @jve:decl-index=0: private JButton messageNote = null; private MessageNote mn = null; // @jve:decl-index=0:visual-constraint="691,36" private JButton hideNote = null; private String notes = ""; // @jve:decl-index=0: private String notes2 = ""; private Font font = null; private Color color = null; private JFileChooser iconFile = null; private ImageIcon icon = null; private static IMPanelClient thisClass = null; private JMenuItem behindColor = null; private JMenuItem foreColor = null; private JMenuItem behindSedColor = null; private JMenuItem foreSedColor = null; private JMenuItem sendIcon = null; private File file = null; private ExampleFileFilter filter = null; private JPopupMenu popMenu2 = null; private JMenuItem personalChatItem = null; private String yourHideLoginName = null; private String yourLoginName = null; public static boolean isSend = false; public static boolean isClick; /** * 初始化分割面板类 */ private JSplitPane getMainSplit() { if (mainSplit == null) { mainSplit = new JSplitPane(); mainSplit.setLeftComponent(getJListScrollPane()); mainSplit.setRightComponent(getReceiveMegScrollPane()); } return mainSplit; } /** * 初始化buttonPane * */ private JPanel getJPanel() { if (jPanel == null) { jPanel = new JPanel(); jPanel.setOpaque(true); jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.X_AXIS)); jPanel.setPreferredSize(new Dimension(246, 30)); jPanel.setCursor(new java.awt.Cursor( java.awt.Cursor.DEFAULT_CURSOR)); jPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0,0,0,0)); jPanel.add(getLogin(), null); jPanel.add(getHideLogin(), null); jPanel.add(getSendMeg(), null); jPanel.add(getLogout(), null); jPanel.add(getExit(), null); jPanel.add(getSendMegScrollPane(), null); } return jPanel; } /** * * 初始化登陆按钮并且修改其属性使其美观,并且添加事件监听器 * */ private JButton getLogin() { if (login == null) { login = new JButton(); login.setText("登陆"); login.setMaximumSize(new Dimension(60, 30)); login.setMinimumSize(new Dimension(60, 30)); login.setName("login"); login.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try { //登陆到服务器 connect("192.168.1.111",6544); /* * 设定各个按钮的状态 * */ hideLogin.setEnabled(false); login.setEnabled(false); logout.setEnabled(true); sendMeg.setEnabled(true); exit.setEnabled(true); hideNote.setEnabled(true); messageNote.setEnabled(false); mn.setVisible(true);//聊天记录为可见 //发送用户昵称 dataOut.writeObject(userName.getText()); dataOut.flush(); yourLoginName = userName.getText(); userName.setText(""); ClientReceiveThread.isStop = false; } catch (IOException e1) { JOptionPane.showMessageDialog(null,"登录失败!!", "系统错误",JOptionPane.ERROR_MESSAGE); System.exit(0); } } }); } return login; } /** * 连接到服务器 * throws IOException * */ protected void connect(String IP,int port){//与服务器端的连接程序 try{ //建立客户端套接字 getClientSocket(IP,port); //得到套接字的数据输出流 getDataOut(); //得到套接字的数据输入流 getDataIn(); //创建客户接受线程 recevThread = new ClientReceiveThread(); recevThread.sendObject(clientSocket,dataOut,dataIn ,receiveMeg, userOnlineList,getSendMeg(),getMegNote(), getSendMegTextPane(),color,icon,statusCheckBox,actionList,getJList()); recevThread.start();//启动监听线程 }catch(IOException e){ JOptionPane.showMessageDialog(this,"连接服务器出错!!", "系统错误",JOptionPane.ERROR_MESSAGE); System.exit(0); } } /** * * 初始化发送信息按钮 * */ private JButton getSendMeg() {//发送信息的按钮 if (sendMeg == null) { sendMeg = new JButton(); sendMeg.setText("发送"); sendMeg.setMaximumSize(new Dimension(60, 30)); sendMeg.setMinimumSize(new Dimension(60, 30)); sendMeg.setName("sendMeg"); sendMeg.setMnemonic(KeyEvent.VK_ENTER); sendMeg.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(clientSocket == null){ return; } if(!clientSocket.isClosed()){ login.setEnabled(false); exit.setEnabled(true); hideLogin.setEnabled(false); logout.setEnabled(true); sendMeg.setEnabled(true); sendMessage();//向所有用户发送消息 sendMegTextPane.setText(""); }else{ return; } } }); } return sendMeg; } /** * 向其他客户端发送信息 * */ private void sendMessage(){ //得到信息发送对象 String toSomebody = userOnlineList.getSelectedItem().toString(); String status = ""; //得到信息发送状态 if(statusCheckBox.isSelected()){ status = "<悄悄话>"; } String action = actionList.getSelectedItem().toString(); String message = sendMegTextPane.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(status);//发送聊天信息状态 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(); } } } /** * This method initializes loginOut * * @return javax.swing.JButton */ private JButton getLogout() {//用户离线按钮 if (logout == null) { logout = new JButton(); logout.setActionCommand(null); logout.setText("离线"); logout.setName("logout"); logout.setMinimumSize(new Dimension(60, 30)); logout.setMaximumSize(new Dimension(60, 30)); logout.setBackground(null); logout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(clientSocket == null){ return; } login.setEnabled(true); sendMeg.setEnabled(false); exit.setEnabled(true); hideLogin.setEnabled(true); logout.setEnabled(false); hideNote.setEnabled(false); messageNote.setEnabled(true); mn.setVisible(false); if(clientSocket.isClosed()){ JOptionPane.showMessageDialog(null,"客户端套接字已经关闭!!" + "请退出!!", "系统警告",JOptionPane.ERROR_MESSAGE); jList.removeAllElements(); ClientReceiveThread.isStop = true; }else if(!clientSocket.isClosed()){ if(isClick==true){ JOptionPane.showMessageDialog(null,"正在私聊,请先退出私聊", "系统警告",JOptionPane.WARNING_MESSAGE); }else{ try{ dataOut.writeObject("用户下线");//向服务器发送用户下线信息 dataOut.flush(); //停止接受线程 ClientReceiveThread.isStop = true; userOnlineList.removeAllItems(); jList.removeAllElements(); jList.addString("秘密会谈中:)"); }catch(IOException ex){ ex.printStackTrace(); } } } } }); } return logout; } /** * This method initializes sendMegTextPane * * @return javax.swing.JTextPane */ protected JTextField getSendMegTextPane() { if (sendMegTextPane == null) { sendMegTextPane = new JTextField();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?