简单仿QQ登陆


这个小程序也算是我程序员的开始

这个小程序主要对swing的知识点作复习

public void showLoginFrame(){
		JFrame loginFrame=new JFrame();
	
  loginFrame.setSize(300,200);
  loginFrame.setTitle("QQ2014");
  //创建流式布局对象
  FlowLayout layout=new FlowLayout();
  loginFrame.setLayout(layout);
  
  //设置窗体初始显示位置居中
  loginFrame.setLocationRelativeTo(null);
  
  JLabel user_name=new JLabel("账户名");
  JLabel user_password=new JLabel("密    码");
  
   JTextField user_field=new JTextField(20);
 JPasswordField  password_field=new JPasswordField(20);
  
  JButton user_reset=new JButton("重置");
  JButton user_login=new JButton("登陆");
  loginFrame.setVisible(true);//使窗口可见
  //
  loginFrame.add(user_name);
  loginFrame.add(user_field);
  loginFrame.add(user_password);
  loginFrame.add(password_field);
  loginFrame.add(user_reset);
  loginFrame.add(user_login);
  //创建动作事件监听对象
  ActionListener action_listener=new ActionListener(){
	  public void actionPerformed(ActionEvent e){
		  String name=user_field.getText();
		  String pwd=password_field.getText();
		  if("123".equals(name)&&"123".equals(pwd)){
			  showIndexFrame();
			  loginFrame.setDefaultCloseOperation(3);
			  loginFrame.setVisible(false);
			  //System.out.println("密码正确");
			  }else{
				System.out.println("密码错误,重新输入!");
			 }
			  
	  }
  
		 
    };
    ActionListener action_listener2=new ActionListener(){
    	public void actionPerformed(ActionEvent e){
    		
    		user_field.setText("");
    		password_field.setText("");
    		
    		
    	}
    	
    	
    	
    };
    //动作事件监听对象的添加
  user_login.addActionListener(action_listener);
  user_reset.addActionListener(action_listener2);
  
  }



这是登陆界面,没有连接数据库

账户名,密码都为JLabel来创建的标签,后面的输入框就是JTextField和JPasswordField

下面两个按钮,然后分别对按钮添加监听就可以了




点击登陆后会进入展示好友列表的方法


循环创建9个按钮,并添加监听

 public void showIndexFrame(){
    	//创建窗体
    	JFrame indexFrame=new JFrame();
    	//设置大小,标题
    	indexFrame.setSize(200,500);
    	indexFrame.setTitle("好友列表");
    	//设置流式分布
    	FlowLayout  layout=new FlowLayout(FlowLayout.CENTER,100,10);
    	indexFrame.setLayout(layout);
    	//设置窗体居中
    	indexFrame.setLocationRelativeTo(null);
    	for(int i=0;i<10;i++){
    		JButton button_friend=new JButton("friend"+i);
    	
    	//创建事件监听对象
    	ActionListener action_listener=new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			showChatFrame();
    			indexFrame.setDefaultCloseOperation(3);
    			indexFrame.setVisible(false);
    		}
    	};
    	button_friend.addActionListener(action_listener);
    	
    	
    	//设置窗体可见
	indexFrame.setVisible(true);
	//添加按钮
	indexFrame.add(button_friend);
    	}
	
    }



同理点击好友的按钮时会调用与好友聊天的方法

 public void showChatFrame(){
    	JFrame chatFrame=new JFrame();
    	chatFrame.setSize(400,400);
    	chatFrame.setTitle("正在聊天中........");
    	//设置流式分布
    	FlowLayout layout=new FlowLayout(FlowLayout.CENTER,0,10);
    	chatFrame.setLayout(layout);
    	//设置窗体居中显示
        	chatFrame.setLocationRelativeTo(null);
    	
    	//设置聊天记录窗体,并设置大小
    	JTextArea area_record=new JTextArea(10,30);
    	JTextArea area_input=new JTextArea(5,30);
    	//设置发送,关闭按钮
    	JButton button_send=new JButton("发送");
    	JButton button_close=new JButton("关闭");
    	chatFrame.add(area_record);
    	chatFrame.add(area_input);
    	chatFrame.add(button_send);
    	chatFrame.add(button_close);
        chatFrame.setVisible(true);
        
        //设置事件监视对象
    	ActionListener action_listener1=new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			area_record.setText(area_record.getText()+"\n"+area_input.getText());
    			
    			String empty="";
    			area_input.setText(empty);
    			
    		}
    		
    		
    		
    		
    		
    	};
    	ActionListener action_listener2=new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    		chatFrame.setDefaultCloseOperation(3);
    			chatFrame.setVisible(false);
    		
    		
    		
    		}
    		
    		
    	};
    	button_send.addActionListener(action_listener1);
    	button_close.addActionListener(action_listener2);
    	
    	
    	
    }



由两个文本域和两个按钮组成,比较简单


以上就是我对写的第一个超过100行代码的简单总结

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值