📄 imguestclient.java
字号:
package com.gamvan.club.users.im.guest;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.Date;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.SwingUtilities;import javax.swing.plaf.basic.BasicPanelUI;import com.gamvan.club.users.im.tools.MyTextPane;public class IMGuestClient extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JPanel buttonPane = null; private JButton login = null; private JButton send = null; private JButton logout = null; private JButton exit = null; private Date date = null; private JTextField sendText = null; private JPanel otherPane = null; private JComboBox userOnlineList = null; private JComboBox actionList = null; private Socket clientSocket = null; // @jve:decl-index=0: private ObjectInputStream dataIn = null; private ObjectOutputStream dataOut = null; private ClientReceiveThread recevThread = null; private JScrollPane receiveMegScrollPane = null; private MyTextPane receiveMeg = null; /** * This method initializes buttonPane * * @return javax.swing.JPanel */ private JPanel getButtonPane() { if (buttonPane == null) { buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(getButtonPane(), BoxLayout.X_AXIS)); buttonPane.add(getLogin(), null); buttonPane.add(getSend(), null); buttonPane.add(getLogout(), null); buttonPane.add(getExit(), null); buttonPane.add(getSendText(), null); } return buttonPane; } /** * This method initializes login * * @return javax.swing.JButton */ private JButton getLogin() { if (login == null) { login = new JButton(); login.setText("登陆"); login.setMinimumSize(new Dimension(60, 60)); login.setMaximumSize(new Dimension(60, 60)); login.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //发送用户信息之前首先发送用户昵称 InetAddress guestIP = null; try { guestIP = InetAddress.getLocalHost(); } catch (UnknownHostException e1) { e1.toString(); } try { connect("192.168.1.111",6544); dataOut.writeObject("过客" + guestIP.getHostAddress().toString()+"(过客)"); dataOut.flush(); login.setEnabled(false); logout.setEnabled(true); exit.setEnabled(true); send.setEnabled(true); } catch (IOException ex) { ex.toString(); } } }); } return login; } private void connect(String IP,int port){ try{ //创建套接字,并直接连接到服务器 getClientSocket(IP,port); //得到套接字的数据输出流 getDataOut(); //得到套接字的数据输入流 getDataIn(); //创建客户接受线程 recevThread = new ClientReceiveThread (clientSocket,dataOut,dataIn, getReceiveMeg(),getUserOnlineList(),getSendText()); recevThread.start();//启动监听线程 }catch(IOException e){ JOptionPane.showMessageDialog(this,"连接服务器出错!!", "系统错误",JOptionPane.ERROR_MESSAGE); System.exit(0); } } /** * This method initializes send * * @return javax.swing.JButton */ private JButton getSend() { if (send == null) { send = new JButton(); send.setEnabled(false); send.setText("发送"); send.setMinimumSize(new Dimension(60, 60)); send.setMaximumSize(new Dimension(60, 60)); send.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //发送信息 login.setEnabled(false); logout.setEnabled(true); exit.setEnabled(true); send.setEnabled(true); if(clientSocket == null){ return; } if(clientSocket != null && !clientSocket.isClosed()){ sendMessage(); sendText.setText(""); } } }); } return send; } private void sendMessage(){ //得到信息发送对象 String toSomebody = userOnlineList.getSelectedItem().toString(); String status = ""; //得到信息发送状态 String action = actionList.getSelectedItem().toString(); String message = sendText.getText(); if(clientSocket.isClosed()){ return; } 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(); dataOut.writeObject(Color.blue); dataOut.flush(); }catch(Exception e){} } /** * This method initializes logout * * @return javax.swing.JButton */ private JButton getLogout() { if (logout == null) { logout = new JButton(); logout.setText("离线"); logout.setEnabled(false); logout.setMinimumSize(new Dimension(60, 60)); logout.setMaximumSize(new Dimension(60, 60)); logout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); login.setEnabled(true); send.setEnabled(false); exit.setEnabled(true); logout.setEnabled(false); if(clientSocket.isClosed()) return; try {// dataOut.writeObject("用户下线"); dataOut.flush(); userOnlineList.removeAllItems(); } catch (IOException ex) { ex.printStackTrace(); } } }); } return logout; } /** * This method initializes exit * * @return javax.swing.JButton */ private JButton getExit() { if (exit == null) { exit = new JButton(); exit.setText("退出"); exit.setMinimumSize(new Dimension(60, 60)); exit.setMaximumSize(new Dimension(60, 60)); exit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { //用户下线 if(clientSocket == null){ System.exit(0); } if(!clientSocket.isClosed()){ try {// dataOut.writeObject("用户下线"); dataOut.flush(); System.exit(0); } catch (IOException ex) { ex.printStackTrace(); } }else if (clientSocket.isClosed()){ System.exit(0); } } }); } return exit; } /** * This method initializes sendText * * @return javax.swing.JTextField */ private JTextField getSendText() { if (sendText == null) { sendText = new JTextField(); sendText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(clientSocket != null&&!clientSocket.isClosed()){ sendMessage(); sendText.setText(""); } } }); } return sendText; } /** * This method initializes otherPane * * @return javax.swing.JPanel */ private JPanel getOtherPane() { if (otherPane == null) { otherPane = new JPanel(); otherPane.setLayout(new BoxLayout(getOtherPane(), BoxLayout.X_AXIS)); otherPane.setUI(new BasicPanelUI()); otherPane.add(getUserOnlineList(), null); otherPane.add(getActionList(), null); } return otherPane; } /** * This method initializes userOnlineList * * @return javax.swing.JComboBox */ private JComboBox getUserOnlineList() { if (userOnlineList == null) { userOnlineList = new JComboBox(); } return userOnlineList; } /** * 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 receiveMegScrollPane * * @return javax.swing.JScrollPane */ private JScrollPane getReceiveMegScrollPane() { if (receiveMegScrollPane == null) { receiveMegScrollPane = new JScrollPane(); receiveMegScrollPane.setViewportView(getReceiveMeg()); } return receiveMegScrollPane; } /** * This method initializes jTextArea * * @return javax.swing.JTextArea */ private MyTextPane getReceiveMeg() { if (receiveMeg == null) { receiveMeg = new MyTextPane(); receiveMeg.setEditable(true); } return receiveMeg; } /** * @param args */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { IMGuestClient thisClass = new IMGuestClient(); thisClass.setVisible(true); } }); } /** * This is the default constructor */ public IMGuestClient() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(451, 341); this.setContentPane(getJContentPane()); this.setTitle("GamVanTalk(Guest)"); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.out.println("windowClosing()"); if(clientSocket == null){ System.exit(0); } if(clientSocket.isClosed()){ System.exit(0); } if(!clientSocket.isClosed()){ try { dataIn.close(); dataOut.close(); clientSocket.close(); System.exit(0); } catch (IOException e1) { e1.printStackTrace(); } } } }); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane(){ if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(getButtonPane(), BorderLayout.SOUTH); jContentPane.add(getOtherPane(), BorderLayout.NORTH); jContentPane.add(getReceiveMegScrollPane(), java.awt.BorderLayout.CENTER); } return jContentPane; } private Socket getClientSocket(String IP,int port) throws UnknownHostException, IOException{ clientSocket = new Socket(IP,port); return clientSocket; } private ObjectOutputStream getDataOut() throws IOException{ dataOut = new ObjectOutputStream( clientSocket.getOutputStream()); dataOut.flush();//发送数据流头 return dataOut; } private ObjectInputStream getDataIn() throws IOException{ dataIn = new ObjectInputStream( clientSocket.getInputStream()); return dataIn; } private Date getDate(){ if(date == null){ date = new Date(); } return date; }} // @jve:decl-index=0:visual-constraint="127,19"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -