“商苑面馆:用户登录界面功能实现”的版本间的差异
来自CloudWiki
(创建页面,内容为“ 任务描述:用户登录界面功能实现 完善用户登录界面功能: 登录成功时,弹出成功界面,如图: 失败时,弹出失败界面,…”) |
(→绑定事件监听器) |
||
(未显示同一用户的25个中间版本) | |||
第1行: | 第1行: | ||
− | + | ==任务描述== | |
完善用户登录界面功能: | 完善用户登录界面功能: | ||
+ | |||
+ | [[文件:Java8-6.png]] | ||
+ | |||
登录成功时,弹出成功界面,如图: | 登录成功时,弹出成功界面,如图: | ||
+ | |||
+ | [[文件:java8-12.png]] | ||
失败时,弹出失败界面,如图: | 失败时,弹出失败界面,如图: | ||
− | + | [[文件:java8-12.png]] | |
− | + | ||
− | + | ==基本实现== | |
− | + | ||
+ | ===创建窗体=== | ||
+ | 具体代码可见 [[Java任务:购书系统登录窗体的设计]] | ||
+ | |||
+ | ===观察有什么事件需要处理=== | ||
+ | 在上述窗体中,有什么事件需要处理呢? | ||
− | + | 很显然,这个窗体很简单,只有两个按钮的点击事件需要处理。 | |
− | public | + | |
− | + | 由前面的知识知道,点击按钮所触发的事件是ActionEvent | |
− | } | + | |
− | + | 我们需要处理ActionEvent事件。 | |
− | + | ||
+ | ===创建事件监听器=== | ||
+ | 接着为其建立监听器。共有两个监听器:一个是登陆按钮的监听器,一个是重置按钮的监听器。 | ||
+ | |||
+ | 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> | ||
− | + | ResetAction: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <nowiki>private class ResetAction implements ActionListener{ | |
− | + | ||
− | + | public ResetAction(){ | |
− | + | ||
− | + | } | |
− | + | @Override | |
− | + | public void actionPerformed(ActionEvent e) { | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
− | |||
− | |||
− | |||
txtUserID.setText(""); | txtUserID.setText(""); | ||
txtPassword.setText(""); | txtPassword.setText(""); | ||
txtUserID.requestFocus(); | txtUserID.requestFocus(); | ||
− | } | + | } |
− | + | }</nowiki> | |
− | + | ||
− | + | ===绑定事件监听器=== | |
− | + | <nowiki> | |
− | + | btnLogon.addActionListener(new LoginAction()); | |
− | + | btnSet.addActionListener(new ResetAction());</nowiki> | |
− | + | ||
− | + | [[文件:java9-41.png]] | |
− | + | ===写一个main方法,测试运行=== | |
− | public | + | <nowiki> |
− | + | public static void main(String[] args) { | |
− | + | UserInfo userInfo=new UserInfo(); | |
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | </nowiki> | ||
+ | ==简化实现(Lambda表达式)== | ||
+ | 在学了[[Lambda表达式]],上述代码还可以这样写,不用单独定义事件监听器了,直接写一个匿名的表达式: | ||
− | + | <nowiki>package main; | |
− | + | import java.awt.*; | |
− | public class | + | import java.awt.event.ActionEvent; |
− | private JLabel | + | import java.awt.event.ActionListener; |
− | private JLabel | + | |
− | 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:创建网格布局 | ||
− | + | centerPane.add(labelUser);//步骤2:将组件添加到面板上 | |
− | + | centerPane.add(txtUserID); | |
− | + | centerPane.add(labelPwd); | |
− | + | centerPane.add(txtPassword); | |
− | + | this.add(centerPane, BorderLayout.CENTER); | |
− | + | //步骤3:以边界布局的方式,将面板整体添加到框架上 | |
− | btnLogon | + | |
− | 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); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
}); | }); | ||
− | + | ||
− | btnReset.addActionListener(e->{ | + | //直接用lambda表达式写事件监听器,点击登陆按钮之后显示信息 |
+ | btnReset.addActionListener(e ->{ | ||
txtUserID.setText(""); | txtUserID.setText(""); | ||
txtPassword.setText(""); | txtPassword.setText(""); | ||
txtUserID.requestFocus(); | txtUserID.requestFocus(); | ||
}); | }); | ||
− | + | ||
− | + | this.add(southPane,BorderLayout.SOUTH); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | this | ||
− | |||
− | |||
− | |||
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) { | ||
− | + | // TODO Auto-generated method stub | |
+ | UserForm userForm=new UserForm(); | ||
} | } | ||
+ | |||
} | } | ||
+ | </nowiki> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | 返回 [[Java程序设计]] |
2020年6月4日 (四) 08:56的最新版本
目录
任务描述
完善用户登录界面功能:
登录成功时,弹出成功界面,如图:
失败时,弹出失败界面,如图:
基本实现
创建窗体
具体代码可见 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());
写一个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程序设计