⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientframe.java

📁 简易聊天工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        wFace = (String)wordFace.getSelectedItem(); 
       	chatWords.setFont(new Font(wFace, Font.PLAIN, wSize));
       	sendToName = (String)sendTo.getSelectedItem();
	}
	void vedioChat_ActionPerformed(ActionEvent event)
	{
		VideoDeviceFrame vdf = new VideoDeviceFrame(this);	
	}
	void audioChat_ActionPerformed(ActionEvent event)
	{
		AudioDeviceFrame adf = new AudioDeviceFrame(this);
	}
	void netMeeting_ActionPerformed()
	{
		CaptureDeviceFrame cdf = new CaptureDeviceFrame(this);
	}
	void sendMsg_ActionPerformed()
	{
		String s = getMsg();
		sendMsg(s);
		chatWords.setText("");
	}
	void exit_ActionPerformed(ActionEvent event)
	{
		disconnect();
		System.exit(0);
	}
	public void disconnect()
	{								// 客户端断开连接方法
	    if(soc != null)
	    {
			try	
			{
				listen.suspend();
				sendMsg("QUIT");				// 发送QUIT信息通知服务器断开此次通讯
				soc.close();									// 关闭套接字
			}
			catch(IOException e) 
			{
		     	System.out.println("Error:" + e);
			}
			finally {}
		}		// end of if
	}
	void sendMsg(String msg)
	{
		try
		{
			out.println(msg);
			out.flush();	
		}catch(Exception e){e.printStackTrace();}
	}
	String getMsg()
	{
		GregorianCalendar d = new GregorianCalendar();
		int year = d.get(Calendar.YEAR);
   		int month = d.get(Calendar.MONTH);
   		int day = d.get(Calendar.DAY_OF_MONTH);
   		int weekday = d.get(Calendar.DAY_OF_WEEK_IN_MONTH);
   		int hour = d.get(Calendar.HOUR_OF_DAY);
   		int min = d.get(Calendar.MINUTE);
   		String time = "";
   		if(min < 10) 
   		{
   			time = "0" + min;
   		}
   		else 
   		{
   			time = "" + min;
   		}
		int red = selectedColor.getRed();
	    int green = selectedColor.getGreen();
	    int blue = selectedColor.getBlue();
	    String sendM = "MSG:" + flag + "|" + sendToName + "|" 
      				+ name + "|" + 
      				year + "|" +
      				month + "|" +
      				day + "|" +
      				weekday + "|" +
      				hour + "|" +
      				time + "|" + 
            		wFace + "|" + 
            		wSize + "|" +
            		red + "|" + green + "|" + blue + "|" + 
            		changeMsg(chatWords.getText());
       // System.out.println(sendM);
        return sendM;
	}
	String changeMsg(String msg)
	{
		int n = msg.indexOf('\n');
		String s = "";
		while(n > 0)
		{
			s += msg.substring(0, n - 1) + "/";
			msg = msg.substring(n + 1);
			n = msg.indexOf('\n');	
		}
		s += msg; 
		System.out.println(s);	
		return s;
	}
	public String findIP(String name)
	{
		for(int i = 0; i < ChatFriends.size(); i++)
		{
			People p = (People)ChatFriends.elementAt(i);
			if(p.name.equals(name)) return p.IP;	
		}
		return null;	
	}
	class SendAction extends AbstractAction//implements ActionListener
   	{
   		
   		public SendAction(String name)
   		{
   			putValue(Action.NAME, name);
   			putValue(Action.SHORT_DESCRIPTION, "发送信息");	
   		}
   		
   		public void actionPerformed(ActionEvent event)
   		{
   			sendMsg_ActionPerformed();
   		}
   	}
   			  	
	public static void main(String[] args)
	{
		//JFrame.setDefaultLookAndFeelDecorated(true);
		//WelcomeFrame frame = new WelcomeFrame();
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setVisible(true);
		String name = JOptionPane.showInputDialog(null, "请输入你的姓名");
		if(name != null)
		{
			ClientFrame frame = new ClientFrame(name);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);	
		}		
	}
}
/*
class WelcomeFrame extends JFrame
{
	public WelcomeFrame()
	{
		setTitle("BITI-Chat");
		setResizable(false);		
		
		Toolkit kit = Toolkit.getDefaultToolkit();
      	Dimension screenSize = kit.getScreenSize();
      	int screenHeight = screenSize.height;
      	int screenWidth = screenSize.width;   	
		setLocation(screenWidth * 2 / 5, screenHeight * 2 / 5);
		setSize(screenWidth / 4, screenHeight / 4);
		 
		 
		WelcomePanel welcomePanel = new WelcomePanel();
		Container contentPane = getContentPane();
		GridBagLayout layout = new GridBagLayout();
		contentPane.setLayout(layout);
      	GridBagConstraints constraints = new GridBagConstraints();
      	
      	UserListener userlistener = new UserListener();
      	
      	un = new JLabel("姓名:");
      	username = new JTextField("");
      	username.addActionListener(userlistener);
      	username.addKeyListener(new KeyAdapter()
      	{
      		public void keyPressed(KeyEvent e)
      		{
      			if(e.getKeyCode() == KeyEvent.VK_ENTER)
      			{
      				password.requestFocusInWindow();	
      			}	
      		}	
      	});
      	
      	
      	pw = new JLabel("密码:");
		password = new JPasswordField();
		
		password.addFocusListener(new FocusAdapter()
		{
			public void focusGained(FocusEvent e)
			{
				password.cut();	
			}	
		});
		
		password.addKeyListener(new KeyAdapter()
      	{
      		public void keyPressed(KeyEvent e)
      		{
      			if(e.getKeyCode() == KeyEvent.VK_ENTER)
      			{
      				conectButton.requestFocusInWindow();	
      			}	
      		}	
      	});     	
		password.addActionListener(userlistener);
		
		
		ConectAction conectlistener = new ConectAction("登 陆");
		conectButton = new JButton(conectlistener);
		conectButton.setFocusable(true);
		conectButton.addActionListener(conectlistener);		
		InputMap imap = conectButton.getInputMap(JComponent.WHEN_FOCUSED);
    	imap.put(KeyStroke.getKeyStroke("ENTER"), "conect");
    	ActionMap amap = conectButton.getActionMap();
    	amap.put("conect", conectlistener);
		
		constraints.fill = GridBagConstraints.NONE;
      	constraints.anchor = GridBagConstraints.EAST;
      	constraints.weightx = 0;
      	constraints.weighty = 0;
		add(un, constraints, 0, 1, 1, 1);
		add(pw, constraints, 0, 2, 1, 1);
		constraints.anchor = GridBagConstraints.CENTER;
		add(conectButton, constraints, 0, 3, 3, 1);	
		
		constraints.fill = GridBagConstraints.HORIZONTAL;
      	constraints.weightx = 100;
		add(username, constraints, 1, 1, 2, 1);
		add(password, constraints, 1, 2, 2, 1);
		
		constraints.weighty = 100;
		constraints.weightx = 100;
		constraints.fill = GridBagConstraints.BOTH;
		add(welcomePanel, constraints, 0, 0, 3, 1);
		
	}
	
	public void add(Component c, GridBagConstraints constraints, int x, int y, int w, int h)
   	{  
      	constraints.gridx = x;
      	constraints.gridy = y;
      	constraints.gridwidth = w;
      	constraints.gridheight = h;
      	getContentPane().add(c, constraints);
   	}
   	
	
	private class UserListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String aname = username.getText();
			char[] apassword = {'0', '0'};
			user = new User(aname, apassword);	
		}	
	}
	
	private class ConectAction extends AbstractAction
	{
		public ConectAction(String name)
		{
			putValue(Action.NAME, name);
   			putValue(Action.SHORT_DESCRIPTION, "登陆服务器");	
		}
		public void actionPerformed(ActionEvent event)
		{
			ClientFrame frame = new ClientFrame(user.getName());
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
			setVisible(false);
		}	
	}
	
	private User user;
	private JLabel un;
	private JLabel pw;
	private JTextField username;
	private JPasswordField password;
	private JButton conectButton;
	
}

class WelcomePanel extends JPanel
{
	public void paintComponent(Graphics g)
	{
		super.paintComponents(g);
		Graphics2D g2 = (Graphics2D)g;
		Font f = new Font("Serif", Font.PLAIN, 20);
		g2.setFont(f);
		g2.drawString("请输入你的姓名和密码!", 20, 50);
		
	}
}
*/
class User
{
   public User(String aName, char[] aPassword)
   {
      name = aName;
      password = aPassword;
   }
   
   public String getName() { return name; }
   public char[] getPassword() { return password; }

   public void setName(String aName) { name = aName; }
   public void setPassword(char[] aPassword) 
   { password = aPassword; }

   private String name;
   private char[] password;
}            

⌨️ 快捷键说明

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