我们日常登录的窗口怎么做呢?

QQ,微信,LOL,或一些软件都有它的登录界面,而这些登录界面往往包含着用户名,密码,验证码这些选项。这里我们来写一个完整的登录窗口。

public class Chengxu {

public static void main(String[] args) {
	//先创建一个窗口,不过此时创建的是隐藏的。
	JFrame jf=new JFrame();
	//再设置窗口大小,单位是像素。
	jf.setSize(800,600);
	//再窗口的标题
	jf.setTitle("Cxj的程序");
	//设置图标
	Toolkit  t=Toolkit.getDefaultToolkit();
	Image image=t.getImage("image\\a.jpg");
	jf.setIconImage(image);
	//设置窗口居中
	jf.setLocationRelativeTo(null);
	//设置窗口的关闭
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	//存放组件,标签组件
	JLabel jl=new JLabel();
	jl.setText("用户名");
	jl.setForeground(Color.CYAN);
	jl.setFont(new Font("华文行楷",Font.BOLD,16));
	jl.setSize(100, 30);
	jl.setLocation(160, 50);//标签的存在位置
	//把标签jl设置在窗口上面
	//一个窗口的面板分为三层   根面板和中间面板 以及内容面板,  组件全部放在内容面板上面
	// 获取到内容面板
	Container con=jf.getContentPane();
	//内容面板的默认布局是麻将布局  东西南北 中  在添加组件的时候如果没有指定放在那里则会放在中间并且把窗口沾满,
	// 重新设置布局
	con.setLayout(null);
	con.add(jl);
	con.setBackground(Color.yellow);//设置窗口颜色
	//设置背景图片
	Toolkit t1=Toolkit.getDefaultToolkit();
	Image image1=t1.getImage("image\\e.jpg");
	Icon  ic=new  ImageIcon(image1);
	JLabel jl3=new JLabel(ic);
	jl3.setSize(800, 600);
	jl3.setLocation(0, 0);
	con.add(jl3);
	//再创造一个密码标签
	JLabel jl1=new JLabel();
	jl1.setText("密    码");
	jl1.setForeground(Color.CYAN);
	jl1.setFont(new Font("华文行楷",Font.BOLD,16));
	jl1.setSize(100, 30);
	jl1.setLocation(160, 130);
	jl3.add(jl1);
	//创建文本框
	JTextField  username=new JTextField();
	username.setSize(233, 30);
	username.setLocation(250, 50);
	jl3.add(username);
	JPasswordField  password=new JPasswordField();
	password.setSize(233, 30);
	password.setLocation(250, 130);
	jl3.add(password);
	//创建验证码标签
	JLabel jl2=new JLabel();
	jl2.setText("验证码");
	jl2.setForeground(Color.CYAN);
	jl2.setFont(new Font("华文行楷",Font.BOLD,16));
	jl2.setSize(100,30);
	jl2.setLocation(160, 200);
	jl3.add(jl2);
	JTextField  yzm1=new JTextField();//创建写验证码的文本框
	yzm1.setSize(100, 30);
	yzm1.setLocation(250, 200);
	jl3.add(yzm1);
	JTextField  yzm2=new JTextField();
	yzm2.setSize(100, 30);
	yzm2.setLocation(380, 200);  //第一次随机产生验证码
	int [] arr1=new int[4];
	for(int i=0;i<4;i++){
		double a=Math.random();
		a=a*10;
		arr1[i]=(int)a;
	}
	yzm2.setText(arr1[0]+""+arr1[1]+""+arr1[2]+""+arr1[3]);//验证码产生
	jl3.add(yzm2);
	JButton huoqu=new JButton();
	huoqu.setText("获取验证码");
	huoqu.setSize(100, 30);
	huoqu.setLocation(490, 200);
	huoqu.setForeground(Color.GRAY);
	jl3.add(huoqu);
	//设置按获取验证码按钮可获得验证码
	
	JButton ok=new JButton();
	ok.setSize(160, 60);
	ok.setLocation(120, 400);
	ok.setText("登录");
	ok.setFont(new Font("华文行楷",Font.PLAIN,25));
	ok.setForeground(Color.green);
	jl3.add(ok);
	JButton cancel=new JButton();
	cancel.setSize(160, 60);
	cancel.setLocation(520, 400);
	cancel.setText("取消");
	cancel.setFont(new Font("华文行楷",Font.PLAIN,25));
	cancel.setForeground(Color.red);
	jl3.add(cancel);
	//设置窗口显示出来
	jf.setVisible(true);
	//给获取验证码按钮一个动作监听器
	huoqu.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			int [] arr2=new int[4];
			for(int j=0;j<4;j++){
				double b=Math.random();
				b=b*10;
				arr2[j]=(int)b;
			}
			yzm2.setText(arr2[0]+""+arr2[1]+""+arr2[2]+""+arr2[3]);
			
		}
	});
	//给ok加一个动作监听器
	ok.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
            String yz1=yzm1.getText(); 
            String yz2=yzm2.getText();
			if(yz1.equals(yz2)){
				String un=username.getText();
				String pw=password.getText();
				
			if(un.equals("admin") && pw.equals("admin")){
				JOptionPane.showMessageDialog(null, "恭喜你登入成功");
				jf.dispose();
			}else{
				JOptionPane.showMessageDialog(null, "用户名或密码错误,请重试");
				password.setText("");
				yzm1.setText("");
				int [] arr2=new int[4];
				for(int j=0;j<4;j++){
					double b=Math.random();
					b=b*10;
					arr2[j]=(int)b;
				}
				yzm2.setText(arr2[0]+""+arr2[1]+""+arr2[2]+""+arr2[3]);
				
			}
          }else{
        	  JOptionPane.showMessageDialog(null, "验证码输入错误,请重输");
        	  yzm1.setText("");
        	  int [] arr2=new int[4];
			for(int j=0;j<4;j++){
				double b=Math.random();
				b=b*10;
				arr2[j]=(int)b;
			}
			yzm2.setText(arr2[0]+""+arr2[1]+""+arr2[2]+""+arr2[3]);
			
          }
		}
	});
	cancel.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			jf.dispose();
			
		}
	});
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值