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

📄 client.java

📁 类似qq的以c/s结构和p2p结构结合的聊天软件。
💻 JAVA
字号:
package com.client;

import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.List;
import java.awt.Color;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Client extends JFrame {

	private static JPasswordField password;
	private static JTextField acounts;
	private static String clientName;
	private static Socket loginSocket;
	private static Socket clientSocket;
	private static ServerSocket serverSocket;
	private static ObjectOutputStream out ;
	private static ObjectInputStream in;
    private static String[] loginMessage;
    private static String[] serverMessage;
    private static String flag;
    private static String dialogMessage;
    public static String serverIP;
    private static int port;
	public static void main(String args[]) {
		try 
		 {
			
			  port=9000;
			  serverIP="127.0.0.1";
			  Client frame = new Client();
			  frame.setTitle("用户登录");
			  frame.setVisible(true);
		 } catch (Exception e) 
		   {
			  e.printStackTrace();
		    }
	      /* try
	       {  
	          serverSocket=new ServerSocket(8890);
	          while(true)
	        	 {
	        		  clientSocket=serverSocket.accept();
	        		  ObjectOutputStream os=new ObjectOutputStream(clientSocket.getOutputStream());
	        		  ObjectInputStream is=new ObjectInputStream(clientSocket.getInputStream());
	        		  String request=(String)is.readObject();
	        		   port++;
	 				   os.writeObject(port);
	 				   os.flush();
	        		  if(request.equals("chat"))
	        		  {
	        			
	        			  flag="server";
		        		  String destIP=clientSocket.getInetAddress().toString();
		        		  ChatThread chat=new ChatThread(flag,destIP,"",port);
		        		  chat.setVisible(true);
					      Thread thread=new Thread(chat);
						  thread.start();
	        		  }
	        		  else if(request.equals("file"))
	        		  {
	        			  
	        			    port=port+1;
		        		    os.writeObject(port);
		        		    os.flush();
	        			    FileRecive fileRcv=new FileRecive(port);
		  	        		fileRcv.setVisible(true);
		  	        		Thread thread=new Thread(fileRcv);
		  	        	    thread.start();
	        		  }
	        	}
	        }catch(Exception e){ e.printStackTrace();}*/
	}
	public Client() 
	{
		super();
		setIconImage(new ImageIcon(this.getClass().getResource("image/bird.jpg")).getImage());
		getContentPane().setLayout(null);
		setBounds(200, 200, 295, 233);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		acounts = new JTextField();
		acounts.setBounds(72, 96, 117, 23);
		getContentPane().add(acounts);

		final JLabel label = new JLabel();
		label.setText("帐 号");
		label.setBounds(24, 96, 42, 20);
		getContentPane().add(label);

		final JLabel label_1 = new JLabel();
		label_1.setText("密 码");
		label_1.setBounds(24, 134, 42, 20);
		getContentPane().add(label_1);

		final JButton register = new JButton();
		register.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				try
				{
				   String cmd="rundll32   url.dll,FileProtocolHandler   http://localhost:8070/bbs/";  
				   Process p=Runtime.getRuntime().exec(cmd);
				}catch(Exception e){ e.printStackTrace();}
			}
		});
		register.setText("注册");
		register.setBounds(202, 95, 72, 23);
		getContentPane().add(register);

		final JButton login = new JButton();
		login.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{   			
				try
			      {
				      loginSocket=new Socket(serverIP,9999);
			       }catch(Exception e)
			               {
			    	           dialogMessage="无法连接服务器";
			    	           MessageDialog dialog=new MessageDialog(dialogMessage);
			    	           dialog.setSize(331, 163);
			    	       }
				
				try
				{
				    out=new ObjectOutputStream(loginSocket.getOutputStream());
				    in=new ObjectInputStream(loginSocket.getInputStream());
				    loginMessage=new String[2];
				    loginMessage[0]=clientName=acounts.getText();
				    loginMessage[1]=password.getText();
				   /*loginMessage[0]=clientName="guosong";
				   loginMessage[1]="0454024";*/
				    out.writeObject(loginMessage);
				    out.flush(); 
				    serverMessage=(String[])in.readObject();
				   if(serverMessage[0].equals("success"))
				   {		
				       MainFrame frame = new MainFrame(serverMessage,clientName,serverIP);
				       Thread thread=new Thread(frame);
				       frame.addWindowListener(new WindowAdapter() 
				       {
						  public void windowClosing(WindowEvent e) 
						  {
							try
							{
								Socket sk=new Socket(serverIP,7777);
						        out=new ObjectOutputStream(sk.getOutputStream());
								in=new ObjectInputStream(sk.getInputStream());
								String[] withdrawMessage=new String[2];
								withdrawMessage[0]="withdraw";
								withdrawMessage[1]=MainFrame.clientName;
								out.writeObject(withdrawMessage);
								out.flush();
								System.exit(0);
							}catch(Exception be){}
						 }
					   });
					   frame.setVisible(true);
					   dispose();
					   thread.start();
				   }
				   else
				   {
					   dialogMessage="帐号或者密码错误!";
	    	           MessageDialog dialog=new MessageDialog(dialogMessage);
	    	           dialog.setSize(331, 163);
	    	           dispose();
				    }
				}catch(Exception e){}
				
			}
		});
		login.setText("登录");
		login.setBounds(202, 133, 72, 23);
		getContentPane().add(login);

		password = new JPasswordField();
		password.setBounds(72, 133, 117, 24);
		getContentPane().add(password);

		final JLabel label_2 = new JLabel();
		label_2.setIcon(new ImageIcon(this.getClass().getResource("image/header.jpg")));		
		label_2.setBounds(0, 0, 287, 73);
		getContentPane().add(label_2);

		final JLabel label_3 = new JLabel();
		label_3.setIcon(new ImageIcon(this.getClass().getResource("image/bar.jpg")));
		label_3.setBounds(0, 79, 287, 9);
		getContentPane().add(label_3);

		final JButton config = new JButton();
		config.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Config config=new Config();
				config.setVisible(true);
			}
		});
		config.setText("服务器配置");
		config.setBounds(34, 170, 103, 23);
		getContentPane().add(config);

		final JButton close = new JButton();
		close.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				System.exit(1);
			}
		});
		close.setText("关闭");
		close.setBounds(155, 170, 99, 23);
		getContentPane().add(close);
		
		//
	}

}

⌨️ 快捷键说明

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