“商苑面馆:用户登录界面功能实现”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“ 任务描述:用户登录界面功能实现 完善用户登录界面功能: 登录成功时,弹出成功界面,如图: 失败时,弹出失败界面,…”)
 
绑定事件监听器
 
(未显示同一用户的25个中间版本)
第1行: 第1行:
任务描述:用户登录界面功能实现
+
==任务描述==
 
完善用户登录界面功能:
 
完善用户登录界面功能:
 +
 +
[[文件:Java8-6.png]]
 +
 
登录成功时,弹出成功界面,如图:
 
登录成功时,弹出成功界面,如图:
 +
 +
[[文件:java8-12.png]]
  
 
失败时,弹出失败界面,如图:
 
失败时,弹出失败界面,如图:
  
8.2.2  任务实现
+
[[文件:java8-12.png]]
8.1.2.1  实施思路
+
 
第一步:启动Eclipse,导入工程Project0801。
+
==基本实现==
第二步:创建User表数据访问接口,在接口中定义登录有效性验证函数,如下图所示方法:
+
 
 +
===创建窗体===
 +
具体代码可见 [[Java任务:购书系统登录窗体的设计]]
 +
 
 +
===观察有什么事件需要处理===
 +
在上述窗体中,有什么事件需要处理呢?
  
具体代码:
+
很显然,这个窗体很简单,只有两个按钮的点击事件需要处理。
public interface UserDao {
+
 
    public User login(String uid,String pwd);
+
由前面的知识知道,点击按钮所触发的事件是ActionEvent
}
+
 
第三步:创建User数据访问类,实现UserDao接口。
+
我们需要处理ActionEvent事件。
public class UserDaoTesting implements UserDao{
+
 
 +
===创建事件监听器===
 +
接着为其建立监听器。共有两个监听器:一个是登陆按钮的监听器,一个是重置按钮的监听器。
 +
 
 +
LoginAction:
 +
 
 +
<nowiki>private class LoginAction implements ActionListener{
 +
 +
public LoginAction(){
 +
 +
}
 +
@Override
 +
public void actionPerformed(ActionEvent e) {
 +
// 获取用户输入的账号和密码
 +
String uid = txtUserID.getText();
 +
String pwd = new String(txtPassword.getPassword());
 +
 +
JOptionPane.showMessageDialog(null, uid+","+pwd, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
}
 +
}</nowiki>
  
@Override
+
ResetAction:
public User login(String uid, String pwd) {
 
// 获取用户测试数据集
 
List<User> users=UserDataSet.users;
 
//循环判断用户输入的账号和密码在测试集合中是否存在
 
for(User user:users)
 
if(user.getId().equalsIgnoreCase(uid)&&user.getPassword().equalsIgnoreCase(pwd))
 
return user;
 
return null;
 
}
 
  
}
+
<nowiki>private class ResetAction implements ActionListener{
第四步:为登录按钮添加点击事件。
+
业务逻辑如下:
+
public ResetAction(){
1)用户点击登录按钮时,获取用户在界面上填写的账号和密码。
+
2)通过用户模拟数据访问类UserDaoTesting验证登录有效性。
+
}
3)登陆成功,通过提示框控件JOptionPane显示提示信息:登录成功。
+
@Override
4)登录失败,通过提示框控件JOptionPane显示提示信息:登录账号或密码不正确
+
public void actionPerformed(ActionEvent e) {
具体代码:
 
btnLogon.addActionListener(e -> {
 
// 获取用户输入的账号和密码
 
String uid = txtUserID.getText();
 
String pwd = new String(txtPassword.getPassword());
 
// 判断用户输入的账号和密码是否存在
 
UserDaoTesting udt = new UserDaoTesting();
 
User user = udt.login(uid, pwd);
 
// 用户编号不存在
 
if (user == null) {
 
JOptionPane.showMessageDialog(this, "登陆账号或密码不正确", "警告信息", JOptionPane.WARNING_MESSAGE);
 
// 增加用户体验,选中文本框中的字,并获得焦点
 
txtUserID.requestFocus();
 
txtUserID.selectAll();
 
} else
 
JOptionPane.showMessageDialog(this, "登陆成功", "信息", JOptionPane.INFORMATION_MESSAGE);
 
});
 
第五步:为重置按钮添加事件。
 
清空用户名和密码,用户名文本框获得焦点,具体代码如下:
 
btnReset.addActionListener(e->{
 
 
txtUserID.setText("");
 
txtUserID.setText("");
 
txtPassword.setText("");
 
txtPassword.setText("");
 
txtUserID.requestFocus();
 
txtUserID.requestFocus();
});
+
}
第六步:运行并测试程序。
+
}</nowiki>
8.1.2.2 程序代码
+
 
1.UserDao.java
+
===绑定事件监听器===
public interface UserDao {
+
  <nowiki>
public User login(String uid,String pwd);
+
btnLogon.addActionListener(new LoginAction());
}
+
btnSet.addActionListener(new ResetAction());</nowiki>
2.UserDaoTesting.java
+
 
public class UserDaoTesting implements UserDao{
+
[[文件:java9-41.png]]
  
@Override
+
===写一个main方法,测试运行===
public User login(String uid, String pwd) {
+
<nowiki>
// 获取用户测试数据集
+
public static void main(String[] args) {
List<User> users=UserDataSet.users;
+
UserInfo userInfo=new UserInfo();
//循环判断用户输入的账号和密码在测试集合中是否存在
 
for(User user:users)
 
if(user.getId().equalsIgnoreCase(uid)&&user.getPassword().equalsIgnoreCase(pwd))
 
return user;
 
return null;
 
 
}
 
}
 +
</nowiki>
 +
==简化实现(Lambda表达式)==
 +
在学了[[Lambda表达式]],上述代码还可以这样写,不用单独定义事件监听器了,直接写一个匿名的表达式:
  
}
+
<nowiki>package main;
3.LogonForm.java
+
import java.awt.*;
public class LogonForm extends JFrame {
+
import java.awt.event.ActionEvent;
private JLabel lblUserID;// “登录账号”标签
+
import java.awt.event.ActionListener;
private JLabel lblPassword;// “登录密码”标签
+
 
private JTextField txtUserID;// “登录账号”文本框
+
import javax.swing.*;
private JPasswordField txtPassword;// 密码框
+
 
private JButton btnLogon;// 登录按钮
+
public class UserForm extends JFrame{
private JButton btnReset;// 重置按钮
+
private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
 +
private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
 +
private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
 +
private JPasswordField txtPassword=new JPasswordField(16);//密码框
 +
private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
 +
    private JButton btnLogon =new JButton("登陆");//登录按钮
 +
private JButton btnReset = new JButton("重置");//重置按钮
 +
 +
public UserForm() {
 +
this.setTitle("商苑面馆-用户登陆");
 +
 +
//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
 +
JPanel centerPane=new JPanel();
 +
centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局
  
public LogonForm() {
+
centerPane.add(labelUser);//步骤2:将组件添加到面板上
// 控件初始化
+
centerPane.add(txtUserID);
lblUserID = new JLabel("用户登录", JLabel.CENTER);
+
centerPane.add(labelPwd);
lblPassword = new JLabel("登录密码", JLabel.CENTER);
+
centerPane.add(txtPassword);
txtUserID = new JTextField(16);
+
this.add(centerPane, BorderLayout.CENTER);
txtPassword = new JPasswordField(16);
+
//步骤3:以边界布局的方式,将面板整体添加到框架上
btnLogon = new JButton("登录");
+
btnLogon.addActionListener(e -> {
+
//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮
// 获取用户输入的账号和密码
+
JPanel southPane=new JPanel();
 +
southPane.setLayout(new FlowLayout());      
 +
southPane.add(btnLogon);
 +
southPane.add(btnReset);
 +
 +
//直接用lambda表达式写事件监听器,点击登陆按钮之后显示信息
 +
btnLogon.addActionListener(e ->{
 
String uid = txtUserID.getText();
 
String uid = txtUserID.getText();
String pwd = new String(txtPassword.getPassword());
+
String pwd = new String(txtPassword.getPassword());
// 判断用户输入的账号和密码是否存在
+
JOptionPane.showMessageDialog(null, uid+","+pwd, "信息", JOptionPane.INFORMATION_MESSAGE);
UserDaoTesting udt = new UserDaoTesting();
 
User user = udt.login(uid, pwd);
 
// 用户编号不存在
 
if (user == null) {
 
JOptionPane.showMessageDialog(this, "登陆账号或密码不正确", "警告信息", JOptionPane.WARNING_MESSAGE);
 
// 增加用户体验,选中文本框中的字,并获得焦点
 
txtUserID.requestFocus();
 
txtUserID.selectAll();
 
} else
 
JOptionPane.showMessageDialog(this, "登陆成功", "信息", JOptionPane.INFORMATION_MESSAGE);
 
 
});
 
});
btnReset = new JButton("重置");
+
btnReset.addActionListener(e->{
+
//直接用lambda表达式写事件监听器,点击登陆按钮之后显示信息
 +
btnReset.addActionListener(e ->{
 
txtUserID.setText("");
 
txtUserID.setText("");
 
txtPassword.setText("");
 
txtPassword.setText("");
 
txtUserID.requestFocus();
 
txtUserID.requestFocus();
 
});
 
});
// 设置控件布局,在窗口中添加用户登录、用户密码控件
+
JPanel centerPane = new JPanel();
+
this.add(southPane,BorderLayout.SOUTH);
centerPane.setLayout(new GridLayout(2, 2));
 
centerPane.add(lblUserID);
 
centerPane.add(txtUserID);
 
centerPane.add(lblPassword);
 
centerPane.add(txtPassword);
 
this.getContentPane().add(centerPane, BorderLayout.CENTER);
 
// 添加登录按钮和重置按钮
 
JPanel southPane = new JPanel();
 
southPane.setLayout(new FlowLayout());
 
southPane.add(btnLogon);
 
southPane.add(btnReset);
 
this.getContentPane().add(southPane, BorderLayout.SOUTH);
 
 
 
pack();
 
this.setTitle("电商购物平台-登录");
 
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
+
        this.setSize(300,150);
 +
this.setVisible(true);
 +
this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
 +
this.setLocationRelativeTo(null);//将窗口设为中央
 
}
 
}
 
+
 +
 
public static void main(String[] args) {
 
public static void main(String[] args) {
LogonForm logon = new LogonForm();
+
// TODO Auto-generated method stub
 +
UserForm userForm=new UserForm();
 
}
 
}
 +
   
 
}
 
}
 +
</nowiki>
 +
 +
 +
 +
 +
返回 [[Java程序设计]]

2020年6月4日 (四) 08:56的最新版本

任务描述

完善用户登录界面功能:

Java8-6.png

登录成功时,弹出成功界面,如图:

Java8-12.png

失败时,弹出失败界面,如图:

Java8-12.png

基本实现

创建窗体

具体代码可见 Java任务:购书系统登录窗体的设计

观察有什么事件需要处理

在上述窗体中,有什么事件需要处理呢?

很显然,这个窗体很简单,只有两个按钮的点击事件需要处理。

由前面的知识知道,点击按钮所触发的事件是ActionEvent

我们需要处理ActionEvent事件。

创建事件监听器

接着为其建立监听器。共有两个监听器:一个是登陆按钮的监听器,一个是重置按钮的监听器。

LoginAction:

private class LoginAction implements ActionListener{
		
		public LoginAction(){
			
		}
		@Override
		public void actionPerformed(ActionEvent e) {
				// 获取用户输入的账号和密码
				String uid = txtUserID.getText();
				String pwd = new String(txtPassword.getPassword());
				
				JOptionPane.showMessageDialog(null, uid+","+pwd, "信息", JOptionPane.INFORMATION_MESSAGE);
		}
	 }

ResetAction:

private class ResetAction implements ActionListener{
		
		public ResetAction(){
			
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			txtUserID.setText("");
			txtPassword.setText("");
			txtUserID.requestFocus();
		}
	 }

绑定事件监听器

btnLogon.addActionListener(new LoginAction());
btnSet.addActionListener(new ResetAction());

Java9-41.png

写一个main方法,测试运行

	public static void main(String[] args) {
		UserInfo userInfo=new UserInfo();
	}

简化实现(Lambda表达式)

在学了Lambda表达式,上述代码还可以这样写,不用单独定义事件监听器了,直接写一个匿名的表达式:

package main;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UserForm extends JFrame{
	private JLabel labelUser=new JLabel("用户登录",JLabel.CENTER);
	private JLabel labelPwd= new JLabel("登录密码",JLabel.CENTER);//“登录密码”标签
	private JTextField txtUserID = new JTextField(16);//“登录账号”文本框
	private JPasswordField txtPassword=new JPasswordField(16);//密码框
	private JLabel labelUser2=new JLabel("@126.com",JLabel.CENTER);
    private JButton btnLogon =new JButton("登陆");//登录按钮
	private JButton btnReset = new JButton("重置");//重置按钮
	
	public UserForm() {
		this.setTitle("商苑面馆-用户登陆");		
		
		//A.创建第一个面板,内部采用网格布局,在其中添加用户登录、用户密码控件
		JPanel centerPane=new JPanel();
		centerPane.setLayout(new GridLayout(2,2));//步骤1:创建网格布局

		centerPane.add(labelUser);//步骤2:将组件添加到面板上
		centerPane.add(txtUserID);
		centerPane.add(labelPwd);
		centerPane.add(txtPassword);
		this.add(centerPane, BorderLayout.CENTER);
		 //步骤3:以边界布局的方式,将面板整体添加到框架上
		
		//B.创建第二个面板,内部采用流式布局,在其中添加用户登录和重置按钮			
		JPanel southPane=new JPanel();
		southPane.setLayout(new FlowLayout());       
		southPane.add(btnLogon);
		southPane.add(btnReset);
		
		//直接用lambda表达式写事件监听器,点击登陆按钮之后显示信息
		btnLogon.addActionListener(e ->{
			String uid = txtUserID.getText();
			String pwd = new String(txtPassword.getPassword());			
			JOptionPane.showMessageDialog(null, uid+","+pwd, "信息", JOptionPane.INFORMATION_MESSAGE);
		});
		
		//直接用lambda表达式写事件监听器,点击登陆按钮之后显示信息
		btnReset.addActionListener(e ->{
			txtUserID.setText("");
			txtPassword.setText("");
			txtUserID.requestFocus();
		});
		
		this.add(southPane,BorderLayout.SOUTH);		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300,150);
		this.setVisible(true);								
		this.setLayout(new BorderLayout());//为框架上的面板设立布局方式,设为边界布局
		this.setLocationRelativeTo(null);//将窗口设为中央
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserForm userForm=new UserForm();
	}
    
}



返回 Java程序设计