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

📄 client.java

📁 聊天室源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import sun.audio.*;
import java.awt.Font;
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;
public class Client extends JFrame implements ActionListener    //获取服务器IP地址
{
    JPanel     p1;  
    JLabel     s,l,a1,a2;
    JTextField ts;
    Button     b1,b2;    
    String serverip;                                    //定义IP
    Dimension scrnsize;                                 //定义显示位置方法
    Toolkit toolkit=Toolkit.getDefaultToolkit();    
    public static void main(String args[])              
	{
		new Client();
	}	    
   public Client()
    {
    	p1=new JPanel();
    	s=new JLabel("请输入你的IP地址:");
    	a1= new JLabel();a2= new JLabel(); 
    	ts =new JTextField(15);
    	b1    =new Button("连接");
    	b2     =new Button("取消");
    	l= new JLabel(new ImageIcon("Chat\\1175563653wt128yns_400_0.jpg")); 
    	this.setTitle("获取服务器IP地址");
    	this.getContentPane().add(p1);
    	p1.setLayout(null);                                           // 定义空布局
    	setCursor(Cursor.getPredefinedCursor(12));                    //设置鼠标形状
    	s.setBounds(50,10,300,25);
    	ts .setBounds(50,60,150,20);
    	b1.setBounds(230,60,75,20);
    	b2.setBounds(230,100,75,20);
    	a1.setBounds(300,80,50,20);
    	a2.setBounds(300,120,50,20);
    	l.setBounds(-20, -80, 440,460);
    	ts.setForeground(Color.red);
    	b1.setForeground(Color.blue);
    	b2.setForeground(Color.blue);
    	s.setForeground(Color.yellow);
    	s.setFont(new Font("黑体",Font.BOLD,18));
    	p1.add(s);
    	p1.add(ts);
    	p1.add(b1);
    	p1.add(b2);
    	p1.add(a1);
    	p1.add(a2);
    	p1.add(l);
    	b1.addActionListener(this);                                // 设置监听
    	b2.addActionListener(this);
    	this.setSize(330,245);                                     //设置界面大小和可见
    	this.show();
    	this.setResizable(false);                                  // 设置最大化不可开启 
    	scrnsize=toolkit.getScreenSize();                          //将窗口定位在屏幕中央
    	this.setLocation(scrnsize.width/2-this.getWidth()/2,
    	                 scrnsize.height/2-this.getHeight()/2);
    }	
    
    public void actionPerformed(ActionEvent e)                     //设置事件监听
    {
    	if(e.getSource()==b1)
    	{
    	    serverip=ts.getText();
    	    this.dispose();
    	    new LoginPanel(serverip);
    	}
    	if(e.getSource()==b2)
    	{
    	    this.dispose();	
    	}
    }
}
class  LoginPanel extends JFrame  implements ActionListener         //登录
{
	static JFrame         fLogin;
	       JPanel         pLogin;
 	       Button         buttonLogin;
 	       Button         buttonRegiste;
 	       Button         buttonClose;
	       JLabel         labelUserName;
	       JLabel         labelPassword;
	       Button         buttonproduce;
	       JLabel         logoimagePosition1;	       
	       JTextField     textFieldUserName;
	       JPasswordField pUserPassword;		
	       String         ipserver;
           AudioStream music;                                       //添加歌曲
           FileInputStream mid;
	       Dimension scrnsize;                                      //用于将窗口用于定位
           Toolkit toolkit=Toolkit.getDefaultToolkit();		   
	public LoginPanel(String ip)
	{
		ipserver=ip;
                try
                   {
                        mid =new FileInputStream("Chat\\谢谢你的爱1999.mid");//添加歌曲
                        music=new AudioStream(mid);
                        AudioPlayer.player.start(music);
                   }
                   catch(Exception ex)
                   {
                         ex.printStackTrace();
                    }		 		
		fLogin=new JFrame("★峰★的聊天室登陆器");  // 添加容器组件
		pLogin=new JPanel();
		fLogin.getContentPane().add(pLogin);
		pLogin.setBackground(Color.white);
		setCursor(Cursor.getPredefinedCursor(12));                //设置鼠标形状
		buttonLogin       =new Button("登录");
		buttonRegiste     =new Button("注册");
		buttonClose       =new Button("重新登录");
		labelUserName     =new JLabel("帐号");
		labelPassword     =new JLabel("密码");
		buttonproduce     =new Button("制作者");				
		textFieldUserName =new JTextField(18);
		pUserPassword     =new JPasswordField(18);		
    	pLogin.setLayout(null);                                    //组件用手动布局								
		labelUserName.setBounds(30,70,150,20);
		textFieldUserName.setBounds(85,70,150,20);
		labelPassword.setBounds(30,100,150,20);
		pUserPassword.setBounds(85,100,150,20);	
		buttonLogin.setBounds(250,170,60,20);
		buttonRegiste.setBounds(250,70,60,20);
		buttonClose.setBounds(90,170,60,20);
		buttonproduce.setBounds(20,170,60,20);
		Font font=new Font("宋体",Font.PLAIN,12);               //设置控件字体大小和
		labelUserName.setFont(new Font("宋体",Font.CENTER_BASELINE,14));
		textFieldUserName.setFont(font);
		labelPassword.setFont(new Font("宋体",Font.CENTER_BASELINE,14));
		pUserPassword.setFont(font);
		buttonLogin.setFont(font);
		buttonRegiste.setFont(font);
		buttonClose.setFont(font);
	    buttonproduce.setFont(font);		
		labelUserName.setForeground(Color.blue);                    //设置控件颜色
		labelPassword.setForeground(Color.blue);				
	    buttonLogin.setForeground(Color.blue);
		buttonRegiste.setForeground(Color.blue);
		buttonClose.setForeground(Color.blue);
	    buttonproduce.setForeground(Color.blue);	                                        //控件添加	   
		pLogin.add(labelUserName);
		pLogin.add(textFieldUserName);
		pLogin.add(labelPassword);
		pLogin.add(pUserPassword);		
		pLogin.add(buttonLogin);
		pLogin.add(buttonRegiste);
		pLogin.add(buttonClose);		
	    pLogin.add(buttonproduce);
		Icon logo1 = new ImageIcon("Chat\\未命名.1.jpg");             //设置背景图片
	 	logoimagePosition1 = new JLabel(logo1);
		logoimagePosition1.setBounds(0, -98, 325, 240);
		pLogin.add(logoimagePosition1);		
		fLogin.setSize(330,245);                                //设置界面大小
		fLogin.setVisible(true);                                //设置界面可见
		fLogin.setResizable(false);                             // 设置最大化不可开启 		
		scrnsize=toolkit.getScreenSize();                       //将窗口定位在屏幕中央
    	fLogin.setLocation(scrnsize.width/2-fLogin.getWidth()/2,scrnsize.height/2-fLogin.getHeight()/2);		
		buttonLogin.addActionListener(this);                 //按钮注册监听 
		buttonRegiste.addActionListener(this);
		buttonClose.addActionListener(this);
		buttonproduce.addActionListener(this);
	} 
	  public void keyReleased(KeyEvent e){}                  //定义空方法
	  public void keyTyped(KeyEvent e){}	                 //定义空方法 
	public void actionPerformed(ActionEvent e)               //事件监听
	{
		Object source=e.getSource();
			if (source.equals(buttonLogin))
		{		                                                      //判断用户名和密码是否为空
	    	if(textFieldUserName.getText().equals("") || pUserPassword.getText().equals(""))
		    {
			    JOptionPane op1=new JOptionPane();
                op1.showMessageDialog(null,"用户名或密码不能为空");                
            }
            else
		    {
			    login();
		    }
		}		
		if (source.equals(buttonproduce))
		{
		JOptionPane op2=new JOptionPane();
        op2.showMessageDialog(null,"制作人:杨峰,彭苗,向珊珊,蒋娥,郭雯,陈平");	
		}																
		if (source.equals(buttonRegiste))                    // 监听到注册界面            
		{
		    fLogin.dispose();
		    new RegistePanel(ipserver);
		}
		if (source == buttonClose)
		{
		    textFieldUserName.setText("");                 //清空
		    pUserPassword.setText("");
		    textFieldUserName.requestFocus();             //光标指到用户名文本处
		}
	}   		
	public void login()                                   //登陆事件响应方法
	{	  
     Customer data=new Customer();                         //接受客户的详细资料
	 data.custName=textFieldUserName.getText();            //定义文件流
	 data.custPassword = pUserPassword.getText();         
	 try
	 {	                                                        //连接服务器端
	   Socket toServer;
  	   toServer = new Socket(ipserver,2008);                //设置套接字
	   ObjectOutputStream streamToServer=new ObjectOutputStream 
	   (toServer.getOutputStream());				      	   
	   streamToServer.writeObject((Customer)data);           //写客户详细资料到服务器socket                   
       BufferedReader fromServer=new BufferedReader(new InputStreamReader
       (toServer.getInputStream()));                         //读来自服务器socket的登陆状态
       String status=fromServer.readLine();                  
           if (status.equals("登陆成功"))
           {
            new chatpanel((String)data.custName,ipserver);
       	    fLogin.dispose(); 
           	    streamToServer.close();                       //关闭流对象
                fromServer.close(); 
                toServer.close();
           }
           else
           {    JOptionPane op=new JOptionPane();
                op.showMessageDialog(null,status);
                streamToServer.close();                        //关闭流对象    
                fromServer.close();
                toServer.close();
           }
         }
		 catch(InvalidClassException e1)                       //捕获异常   登录结束
		 {
		    System.out.println("The Customer class is invalid" + e1);
		 }
		 catch(NotSerializableException e2)
		 {
		    System.out.println("The object is not serializable" + e2);
		 }
		 catch(IOException e3)
		 {
		    System.out.println("Cannot write to the server" + e3);
		 }
	}   	
}         
class RegistePanel extends JFrame  implements ActionListener            //注册
{
	static JFrame          fRegiste;
	       JPanel          pRegiste;
           JLabel          labelTitle;
           JLabel          labelUserName;
           JLabel          labelSex;
           JLabel          labelPassWord;
           JLabel          labelConfirm;
           JLabel          labelEmail;         
           JLabel          labelAdd;
           JLabel          labelPhone;
           JLabel          labelAge;
           JLabel          b2;	
           JLabel   z0,z1,z2,z3,z4,z5,z6,z7,b1;          
           JLabel          logoimagePosition2;
           JTextField      textFieldUserName;
           JTextField      textEmail;           
           JTextField      textAdd;
           JTextField      textPhone;
           JTextField      textAge;           
           JPasswordField  pUserPassword;
           JPasswordField  pConfirmwd;
           JRadioButton    radioMan;
           JRadioButton    radioWoman;
           ButtonGroup     buttongroupSex;
           Button          buttonOk;
           Button          buttonCancel;
           Button          buttonClose;          
           String          ipserver;            
		   Dimension scrnsize;                           //用于将窗口用于定位IP定义
           Toolkit toolkit=Toolkit.getDefaultToolkit();//将窗口定位在屏幕中央    
	public RegistePanel(String ip)
	{				
		fRegiste=new JFrame("★峰★聊天室注册器");
		pRegiste=new JPanel();
		fRegiste.getContentPane().add(pRegiste);
		setCursor(Cursor.getPredefinedCursor(12));                 //设置鼠标形状	
		labelTitle=new JLabel("为了方便您的使用请先注册!");
		labelUserName=new JLabel("用 户 名:");
		labelSex=new JLabel("性    别:");
		labelPassWord=new JLabel("用户密码:");
		labelConfirm=new JLabel("确认密码:");
		labelEmail=new JLabel("电子邮件:");
		radioMan=new JRadioButton("男",true);
		radioWoman=new JRadioButton("女");
		labelAge=new JLabel("年    龄:");
		labelPhone=new JLabel("电话号码:");
		labelAdd=new JLabel("地    址:");
		z1=new JLabel("*    请输入别名");z1.setForeground(Color.red);z2=new JLabel("*    请输入密码");z2.setForeground(Color.red);
        z3=new JLabel("*    请再次输入密码");z3.setForeground(Color.red);
        z4=new JLabel("可以不填");z4.setForeground(Color.red);
        z5=new JLabel("*    请选择性别");z5.setForeground(Color.red);
        z6=new JLabel("可以不填");z6.setForeground(Color.red);
        z7=new JLabel("*    请输入邮箱");z7.setForeground(Color.red);        
        z0=new JLabel("可以不填");z0.setForeground(Color.red);
        b1=new JLabel("带*项的是必须填写的");
        b2=new JLabel("谢谢您的注册!");
        b1.setForeground(Color.green);
        b2.setForeground(Color.green);
		textFieldUserName=new JTextField(30);
		textEmail=new JTextField(30);
		pUserPassword=new JPasswordField(30);
		pConfirmwd=new JPasswordField(30);

⌨️ 快捷键说明

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