impanelclient.java

来自「注:语音聊天部分还未完成」· Java 代码 · 共 1,254 行 · 第 1/3 页

JAVA
1,254
字号
			fontSet.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					font = NwFontChooserS.showDialog(							thisClass,"字体设置",font);					receiveMeg.setFont(font);					//setFont = true;				}			});		}		return fontSet;	}	/**	 * This method initializes popMenu1	 *	 * @return javax.swing.JPopupMenu	 */private	JPopupMenu getPopMenu1() {		if (popMenu1 == null) {			popMenu1 = new JPopupMenu();			popMenu1.setVisible(true);			popMenu1.add(getSetsedFont());			popMenu1.add(getBehindSedColor());			popMenu1.add(getForeSedColor());			popMenu1.add(getSendIcon());		}		return popMenu1;	}	/**	 * This method initializes setsedFont	 *	 * @return javax.swing.JMenuItem	 */	protected JMenuItem getSetsedFont() {		if (setsedFont == null) {			setsedFont = new JMenuItem("字体设置");			setsedFont.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					font = NwFontChooserS.showDialog(							thisClass,"字体设置",font);					sendMegTextPane.setFont(font);				}			});		}		return setsedFont;	}	/**	 * This method initializes behindColor	 *	 * @return javax.swing.JMenuItem	 */	protected JMenuItem getBehindColor() {		if (behindColor == null) {			behindColor = new JMenuItem("设置被景色");			behindColor.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					System.out.println("actionPerformed()");					color = JColorChooser.showDialog(foreSedColor, "被景色", color);					receiveMeg.setBackground(color);				}			});		}		return behindColor;	}	/**	 * This method initializes foreColor	 *	 * @return javax.swing.JMenuItem	 */	private JMenuItem getForeColor() {		if (foreColor == null) {			foreColor = new JMenuItem("设置前景色");			foreColor.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					color = JColorChooser.showDialog(							receiveMeg, "前景色", color);					MyTextPane.setColor = true;				}			});		}		return foreColor;	}	/**	 * This method initializes behindSedColor	 *	 * @return javax.swing.JMenuItem	 */	protected JMenuItem getBehindSedColor() {		if (behindSedColor == null) {			behindSedColor = new JMenuItem("设置被景色");			behindSedColor.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					color = JColorChooser.showDialog(foreSedColor, "被景色", color);					sendMegTextPane.setBackground(color);				}			});		}		return behindSedColor;	}	/**	 * This method initializes foreSedColor	 *	 * @return javax.swing.JMenuItem	 */	protected JMenuItem getForeSedColor() {		if (foreSedColor == null) {			foreSedColor = new JMenuItem("设置前景色");			foreSedColor.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {					color = JColorChooser.showDialog(							sendMegTextPane, "前景色", color);					sendMegTextPane.setForeground(color);				}			});		}		return foreSedColor;	}	private JMenuItem getSendIcon(){//发送图片		if(sendIcon == null){			sendIcon = new JMenuItem("发送图片");			sendIcon.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,					InputEvent.CTRL_MASK,true));			sendIcon.setMnemonic(KeyEvent.VK_U);			sendIcon.addActionListener(new java.awt.event.ActionListener(){				public void actionPerformed(ActionEvent arg0) {					try {						getIconFile();					} catch (IOException e) {						JOptionPane.showMessageDialog(		                 		null,"必须发送图片!!",		                 		"错误信息",JOptionPane.ERROR_MESSAGE);						 System.exit(0);					}				}			});		}		return sendIcon;	}	private ExampleFileFilter getFilter(){		filter = new ExampleFileFilter(new String[]{"gif","jpg"},"GIF & jpg Images");		return filter;	}		private JFileChooser getIconFile() throws IOException{		iconFile = new JFileChooser();		iconFile.setCurrentDirectory(file);		//获得过滤器		getFilter();		iconFile.addChoosableFileFilter(filter);		iconFile.setFileFilter(filter);		iconFile.setAcceptAllFileFilterUsed(false);		int result = iconFile.showOpenDialog(this);		if(result == JFileChooser.APPROVE_OPTION){//获得用户行为			if(clientSocket == null){				JOptionPane.showMessageDialog(						null,"请先登陆!!",						"错误信息",JOptionPane.WARNING_MESSAGE);			}			if(clientSocket != null){				file = iconFile.getSelectedFile();												try {					if(file != null&&file.length()<20000){						icon = new ImageIcon(file.getPath());					}else{						JOptionPane.showMessageDialog(								null,"图片过大,无法发送!!",								"错误信息",JOptionPane.WARNING_MESSAGE);					}					if(icon != null){						dataOut.writeObject("发送图片");						dataOut.flush();						dataOut.writeObject(icon);						dataOut.flush();					}										}catch (MalformedURLException e) {					System.exit(0);				}							}		}		return iconFile;	}	/**	 * This method initializes popMenu2	 *	 * @return javax.swing.JPopupMenu	 */	private JPopupMenu getPopMenu2() {		if (popMenu2 == null) {			popMenu2 = new JPopupMenu();			popMenu2.add(getPersonalChatItem());		}		return popMenu2;	}	/**	 * This method initializes addFriendItem	 *	 * @return javax.swing.JMenuItem	 */	private JMenuItem getPersonalChatItem(){		if (personalChatItem == null) {						personalChatItem = new JMenuItem();			personalChatItem.setText("私聊");			personalChatItem.addActionListener(new java.awt.event.ActionListener() {				public void actionPerformed(java.awt.event.ActionEvent e) {										sendPersonalChatRequest();//发送私聊请求                                                                            				}			});		}		return personalChatItem;	}	private void sendPersonalChatRequest(){		int index = userOnlineList.getSelectedIndex();				String str = userOnlineList.getSelectedItem().toString();                         		if(str.endsWith("(管理员)")||str.endsWith("(过客)")){                                           			JOptionPane.showMessageDialog(		                 							null,"对不起,您不能和管理员或者过客私聊!",             		"警告信息",JOptionPane.WARNING_MESSAGE);                                           			return;                                       		}		if(isSend == true){						String add = FormatDateTime.dateAdd("s", 60, "yyyy-MM-dd HH:mm:ss");			boolean isTime = FormatDateTime.secondCompare(add);			if(isTime == false){				JOptionPane.showMessageDialog(                 		null,"发送请求太快了,过一分钟秒再说吧:)",                 		"警告信息",JOptionPane.WARNING_MESSAGE);				isSend = false;				return;			}					}		if(index ==0||str.equalsIgnoreCase(yourHideLoginName)				||str.equalsIgnoreCase(yourLoginName)){			JOptionPane.showMessageDialog(             		null,"请选择正确的私聊对象!!!",             		"警告信息",JOptionPane.WARNING_MESSAGE);		}else{			try {				dataOut.writeObject("私聊");				dataOut.flush();				dataOut.writeInt(index);				dataOut.flush();				JOptionPane.showConfirmDialog(                 		null,"信息已成功送出,稍等一会儿" + "\n" +                  		"如果对方长时间没有回应则是拒绝了您的请求!",                 		"系统信息",JOptionPane.CLOSED_OPTION,                 		JOptionPane.INFORMATION_MESSAGE);				isSend = true;							} catch (IOException e1) {				JOptionPane.showMessageDialog(                 		null,"非法操作,程序被意外关闭!",                 		"警告信息",JOptionPane.WARNING_MESSAGE);				System.exit(0);			}		}	}	/**	 * 主方法	 */	public static void main(String args[]) {		            SwingUtilities.invokeLater(new Runnable() {			                                   public void run() {		                                                                           	                    thisClass = new IMPanelClient();				                                            thisClass.setVisible(true);					// Is your UI already created? So you will have to update the component-tree					// of your current frame (or actually all of them...)			            				                }			     		});	}	/**	 * This is the default constructor	 */	public IMPanelClient() {		super();		initialize();	}	/**	 * This method initializes this	 * 初始化本类	 * return void	 */	protected void initialize() {		this.setSize(600,400);		//设置组件语言环境		this.setLocale(new Locale("zh", "CN", ""));		this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));		this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);		this.setName("Client");		this.setContentPane(getJContentPane());		this.setTitle("GamvanTalk");		this.setVisible(true);		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 {						ClientReceiveThread.isStop = true;                        dataOut.writeObject("用户下线");                        dataOut.flush();                        System.exit(0);					} catch (IOException ex) {                        ex.printStackTrace();                	}				}else if(clientSocket != null && clientSocket.isClosed()){					System.exit(0);				}			}		});	}	/**	 * This method initializes jContentPane	 * 初始化底层的面板	 * @return javax.swing.JPanel	 */	private JPanel getJContentPane() {		if (jContentPane == null) {			jContentPane = new JPanel();			jContentPane.setLayout(new BorderLayout());			jContentPane.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));			jContentPane.add(getMainSplit(), java.awt.BorderLayout.CENTER);			jContentPane.add(getJPanel(), java.awt.BorderLayout.SOUTH);			jContentPane.add(getOtherPane(), java.awt.BorderLayout.NORTH);		}		return jContentPane;	}	/**	 * 初始化客户端套接字	 * 返回socket	 * */	protected Socket getClientSocket(String IP, int port)		throws IOException{	//创建安全的客户端套结字		clientSocket = new Socket(IP,port);		//使包能够尽快地发送		clientSocket.setTcpNoDelay(true);		return clientSocket;	}	/**	 * 初始化数据输出流	 * 返回ObjectOutputStream	 * */	protected ObjectOutputStream getDataOut() throws IOException{		dataOut = new ObjectOutputStream(				clientSocket.getOutputStream());		dataOut.flush();//发送数据流头		return dataOut;	}	/**	 * 初始化数据输入流	 * 返回ObjectInputStream	 * */	protected ObjectInputStream getDataIn() throws IOException{		 //得到套接字的数据输入流		dataIn = new ObjectInputStream(				clientSocket.getInputStream());		return dataIn;	}	protected int getHideLoginCount(){		hideLoginCount = new Random();		int count = hideLoginCount.nextInt(500);		return count;	}	protected MessageNote getMegNote(){//聊天纪录类		if(mn == null){			mn = new MessageNote();			mn.setTitle("聊天记录");		}		return mn;	}	private Date getDate(){		if(date == null){			date = new Date();		}		return date;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?