📄 impaneladminclient.java
字号:
return infoTextPane; } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getExit() { if (exit == null) { exit = new JButton(); exit.setText("退出"); exit.setMaximumSize(new java.awt.Dimension(60,60)); exit.setMinimumSize(new java.awt.Dimension(60,60)); exit.setPreferredSize(new java.awt.Dimension(60,28)); exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(clientSocket == null){ System.exit(0); }else if(clientSocket != null && !clientSocket.isClosed()){ //如果套接字没有关闭或者服务器没有关闭 try { recevThread.isStop = true; dataOut.writeObject("用户下线"); dataOut.flush(); System.exit(0); } catch (IOException ex) { ex.printStackTrace(); } }else if(clientSocket != null && clientSocket.isClosed()){ System.exit(0); } } }); } return exit; } /** * This method initializes otherPane * 初始化otherPane * @return javax.swing.JPanel */ private JPanel getOtherPane() { if (otherPane == null) { otherPane = new JPanel(); otherPane.setLayout(new BoxLayout(getOtherPane(), BoxLayout.X_AXIS)); otherPane.add(getUserName(), null); otherPane.add(getUserOnlineList(), null); otherPane.add(getActionList(), null); otherPane.add(getMessageNote(), null); otherPane.add(getHideNote(), null); otherPane.add(getStatusCheckBox(), null); } return otherPane; } /** * This method initializes userName * * @return javax.swing.JTextField */ private JTextField getUserName() { if (userName == null) { userName = new JTextField(); userName.setText("请在这里输入管理员名称"); userName.setAlignmentX(10.0F); userName.setAlignmentY(10.0F); userName.setMinimumSize(new java.awt.Dimension(90,90)); userName.setMaximumSize(new java.awt.Dimension(300,300)); userName.setPreferredSize(new java.awt.Dimension(136,20)); userName.setToolTipText("请输入昵称"); } return userName; } /** * This method initializes userOnlineList * * @return javax.swing.JComboBox */ private JComboBox getUserOnlineList() { if (userOnlineList == null) { userOnlineList = new JComboBox(); userOnlineList.setToolTipText("在线用户列表"); userOnlineList.setMinimumSize(new java.awt.Dimension(10,10)); userOnlineList.addMouseListener(new java.awt.event.MouseListener() { public void mouseClicked(java.awt.event.MouseEvent e) { } public void mousePressed(java.awt.event.MouseEvent e) { if(e.getModifiers() == InputEvent.META_MASK){ getPopMenu(); popMenu.show(userOnlineList,e.getX(),e.getY()); } } public void mouseReleased(java.awt.event.MouseEvent e) { } public void mouseEntered(java.awt.event.MouseEvent e) { } public void mouseExited(java.awt.event.MouseEvent e) { } }); userOnlineList.addItem("所有人"); } return userOnlineList; } /** * This method initializes statusCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getStatusCheckBox() { if (statusCheckBox == null) { statusCheckBox = new JCheckBox(); statusCheckBox.setText("悄悄话"); } return statusCheckBox; } /** * This method initializes actionList * 初始化表情列表 * @return javax.swing.JComboBox */ private JComboBox getActionList() { if (actionList == null) { actionList = new JComboBox(); actionList.addItem("微笑的"); actionList.addItem("小心的"); actionList.addItem("生气的"); } return actionList; } /** * This method initializes popMenu * * @return javax.swing.JPopupMenu */ private JPopupMenu getPopMenu() { if (popMenu == null) { popMenu = new JPopupMenu(); popMenu.add(getWarningUser()); popMenu.add(getKickUser()); } return popMenu; } /** * This method initializes kickUser * @return javax.swing.JMenuItem */ private JMenuItem getKickUser() { if (kickUser == null) { kickUser = new JMenuItem("踢出"); infoTextPane.setText("通过单击踢人菜单您可以踢出反纪律的人!" +"单击警告菜单可以警告已经轻微违反大厅纪律的人"); kickUser.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { String kickUser = userOnlineList.getSelectedItem().toString(); int kickIndex = userOnlineList.getSelectedIndex(); if(kickIndex == 0){ return; }else{ try { dataOut.writeObject("kicked!");//首先发送踢人的命令符 dataOut.flush(); //再发送要踢出的人 dataOut.writeObject(kickUser); dataOut.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } }); } return kickUser; } /** * This method initializes warningUser * * @return javax.swing.JMenuItem */ private JMenuItem getWarningUser() {//警告用户快捷菜单 if (warningUser == null) { warningUser = new JMenuItem("警告"); infoTextPane.setForeground(Color.RED); warningUser.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { String toSomebody = userOnlineList.getSelectedItem().toString(); try {//向服务器发送警告信息 if(toSomebody.equalsIgnoreCase("所有人")){ return; } dataOut.writeObject("警告"); dataOut.flush(); //向服务器发送警告的对象 dataOut.writeObject(toSomebody); dataOut.flush(); } catch (IOException e1) { e1.printStackTrace(); } } }); } return warningUser; } /** * This method initializes messageNote * * @return javax.swing.JButton */ private JButton getMessageNote() {//显示用户聊天记录 if (messageNote == null) { messageNote = new JButton(); messageNote.setEnabled(false); messageNote.setText("显示记录"); messageNote.setMinimumSize(new java.awt.Dimension(86,60)); messageNote.setMaximumSize(new java.awt.Dimension(86,60)); messageNote.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); if(clientSocket == null){ return; } //把最近聊天记录传送给聊天记录文本区域 mn.setVisible(true);//设定聊天记录为不可见 notes = receiveMeg.getText(); mn.getMegNote().setText(notes2);//让聊天记录文本区域显示最新的聊天记录 mn.getMegNote().append(Color.red,notes); hideNote.setEnabled(true); messageNote.setEnabled(false); } }); } return messageNote; } /** * This method initializes hideNote * * @return javax.swing.JButton */ private JButton getHideNote() {//隐藏聊天记录 if (hideNote == null) { hideNote = new JButton(); hideNote.setText("隐藏记录"); hideNote.setEnabled(false); hideNote.setMinimumSize(new java.awt.Dimension(86,60)); hideNote.setMaximumSize(new java.awt.Dimension(86,60)); hideNote.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(clientSocket == null){ return; } mn.setVisible(false); hideNote.setEnabled(false); messageNote.setEnabled(true); } }); } return hideNote; } /** * This method initializes popMenu1 * * @return javax.swing.JPopupMenu */ private JPopupMenu getPopMenu1() {//清屏弹出示菜单 if (popMenu1 == null) { popMenu1 = new JPopupMenu(); popMenu1.add(getClearText()); } return popMenu1; } /** * This method initializes clearText * * @return javax.swing.JMenuItem */ private JMenuItem getClearText() {//清屏菜单 if (clearText == null) { clearText = new JMenuItem("清空"); clearText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { notes2 = receiveMeg.getText(); receiveMeg.setText(""); } }); } return clearText; } /** * 主方法 */ public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { IMPanelAdminClient thisClass = new IMPanelAdminClient(); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public IMPanelAdminClient() { super(); initialize(); } /** * This method initializes this * 初始化本类 * @return void */ private void initialize() { this.setSize(600, 400); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.setName("admin"); this.setContentPane(getJContentPane()); this.setTitle("GamVanTalk(Administor)"); //监听窗体关闭按钮事件 this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { if(clientSocket == null){ System.exit(0); }else if(clientSocket != null && !clientSocket.isClosed()){ //如果套接字没有关闭或者服务器没有关闭 try { recevThread.isStop = true; dataOut.writeObject("用户下线"); dataOut.flush(); System.exit(0); } catch (IOException ex) { ex.printStackTrace(); } }else if(clientSocket != null && clientSocket.isClosed()){ System.exit(0); } } }); this.setVisible(true); } /** * This method initializes jContentPane * 初始化底层的面板 * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getMainSplit(), java.awt.BorderLayout.CENTER); jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH); jContentPane.add(getOtherPane(), java.awt.BorderLayout.NORTH); } return jContentPane; } /** * 初始化客户端套接字 * 返回socket * */ public Socket getClientSocket(String ip,int port) throws IOException{ clientSocket = new Socket(ip,port); return clientSocket; } /** * 初始化数据输出流 * 返回ObjectOutputStream * */ public ObjectOutputStream getDataOut() throws IOException{ dataOut = new ObjectOutputStream( clientSocket.getOutputStream()); dataOut.flush();//发送数据流头 return dataOut; } /** * 初始化数据输入流 * 返回ObjectInputStream * */ private ObjectInputStream getDataIn() throws IOException{ //得到套接字的数据输入流 dataIn = new ObjectInputStream( clientSocket.getInputStream()); return dataIn; } private MessageNote getMegNote(){ if(mn == null){ mn = new MessageNote(); mn.setTitle("聊天记录"); } return mn; } private Date getDate(){ if(date == null){ date = new Date(); } return date; }} // @jve:decl-index=0:visual-constraint="83,7"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -