JAVA 登录界面UI

之前的UI思路有一点问题,UI的实现和监听放在一个类里就可以的,否则监听调文本框中的文本会很麻烦XD

所以登录界面代码如下:

public class GUI_login implements ActionListener
{
//先将UI中的所有组件定义
	static String username_input;
	static String password_input;
	static boolean flag_login;
	//GUI界面
	JFrame login_frame;//基本面板
	JPanel login_panel;
	JTextField username_text;
	JPasswordField password_text;
	Button login_button;
	Button register_button;
	JLabel username_label;
	JLabel password_label;

//监听,登录和注册按钮

    public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand()=="登录")
		{
			username_input=username_text.getText();
			password_input=String.valueOf(password_text.getPassword());
			password_check();
			password_input=null;//安全处理,保存密码的字符串清零
		}

		if(e.getActionCommand()=="注册")
		{
			System.out.println("还没做呢别急");
		}
		if(flag_login==true)
		{
			System.out.println("跳转中");
			//跳转至选择界面
		}
	}

//具体UI,单纯写的好玩
	public GUI_login()
	{
		login_frame=new JFrame();//基本面板
		login_panel=new JPanel();
		login_frame.add(login_panel);
		
		login_frame.setBounds(256,256,512,288);

		Color c_background=new Color(255,255,205);//颜色配置
		login_panel.setBackground(c_background);
		login_panel.setSize(512,288);
		//label设计
		username_label=new JLabel("用户名:");
		password_label=new JLabel("密码:");
		username_label.setBounds(140, 69, 60, 20);
		password_label.setBounds(150, 99, 60, 20);
		
		//button设计
		login_button=new Button("登录");
		register_button=new Button("注册");
		login_button.setVisible(true);
		login_button.addActionListener(this);
		login_button.setBounds(190, 130, 60, 20);
		
		register_button.setVisible(true);
		register_button.addActionListener(this);
		register_button.setBounds(280, 130, 60, 20);
		
		//text设计
		username_text=new JTextField(0);
		username_text.setBounds(190, 70, 150, 20);
		
		password_text=new JPasswordField(0);
		password_text.setBounds(190, 100, 150, 20);
		
		//panel设计
		login_panel.add(username_text);
		login_panel.add(password_text);
		login_panel.add(username_label);
		login_panel.add(password_label);
		login_panel.add(login_button);
		login_panel.add(register_button);
		login_panel.setLayout(null);
		
		//frame设计
		login_frame.setLayout(null);
        login_frame.setTitle("I said the calculation!自动出题系统!—登录");
		login_frame.setResizable(true);
		login_frame.setVisible(true);
		login_frame.validate();
		login_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

//密码比对,改起来也简单
	public static void password_check()
	{
		try
		{
			File autopro_dir=new File("D:\\autopro");
			if(!autopro_dir.exists())
			{
				autopro_dir.mkdir();
			}
			File password_dir=new File("D:\\autopro\\password");
			if(!password_dir.exists())
			{
				password_dir.mkdir();
			}
			File password_file=new File("D:\\autopro\\password\\password.txt");
			if(!password_file.exists())
			{
				password_file.createNewFile();
			}
			//以上password文件生成
			
			String password_path;
			password_path="D:\\autopro\\password\\password.txt";
			InputStreamReader password_reader=new InputStreamReader(new FileInputStream(password_path));
			BufferedReader password_br=new BufferedReader(password_reader);
			String password_line="";
			String[] password_unp=new String[2];//用户名和密码
			while((password_line = password_br.readLine()) != null)
			{
				password_unp=password_line.split(";");//以;分隔,因此密码中不可以有;
				if(password_unp[0].equals(username_input))
				{
					if(password_unp[1].equals(password_input))
					{
						flag_login=true;
					}
				}
			}
			password_br.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("Password check failed.");
		}
	}

//主函数
	public static void main(String[] args)
	{
		//GUI_login UI_login0=new GUI_login();
		new GUI_login();
	}
}

简单的登录UI就这样了,麻雀虽小也算五脏俱全了,快乐。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值