目录
分层开发实现注册功能—服务层之父接口 UserService
我们将使用前边所学的控件模拟实现一个简单的用户注册的验证功能。需求如下:用户姓名必须填写,密码不能为空,兴趣爱好最少选择一项。验 证时如果哪一项没有通过,弹出对话框,显示对应错误;如果验证通过了,使用对话框显示 所有填入的信息。
同样采用之前分层开发的思想来实现
分层开发实现注册功能—服务层之父接口 UserService
import java.awt.Window;
import java.util.EventObject;
public interface UserService {
void execute(EventObject e,Window w);
}
分层开发实现注册功能—服务层之注册按钮服务层
package com.gui;
import java.awt.Window;
import java.util.EventObject;
import javax.swing.JOptionPane;
/**
* 注册功能服务层.
*/
public class RegistService implements UserService {
@Override
public void execute(EventObject e, Window w) {
RegistUserFrame ruf = (RegistUserFrame)w;
//获得各个控件的值
String userName = ruf.getTxtUserName().getText().trim();
String userPwd = new String(ruf.getPwdUserPwd().getPassword()).trim();
String sex = "";
//判断性别哪个控件被选中,获得选中的文本
if(ruf.getRabM().isSelected()) {
sex = ruf.getRabM().getText();
}else {
sex = ruf.getRabF().getText();
}
//断言兴趣都没选中
boolean bool = false;
String interest = "";
//只要有一个兴趣被选中就将断言改为 true,并获得选中的文本
if(ruf.getChkTV().isSelected()) {
bool = true;
interest += ruf.getChkTV().getText()+" ";
}
if(ruf.getChkGame().isSelected()) {
bool = true;
interest += ruf.getChkGame().getText()+" ";
}
if(ruf.getChkStudy().isSelected()) {
bool = true;
interest += ruf.getChkStudy().getText()+" ";
}
if(ruf.getChkOther().isSelected()) {
bool = true;
interest += ruf.getChkOther().getText()+" ";
}
String degrees = (String)ruf.getCmbDegrees().getSelectedItem();
//开始验证
if(userName.equals("")) {
JOptionPane.showMessageDialog(null, "请输入用户名", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if(userPwd.equals("")) {
JOptionPane.showMessageDialog(null, "请输入密码", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
if(!bool) {
JOptionPane.showMessageDialog(null, "请选择最少一个兴趣", "错误",
JOptionPane.ERROR_MESSAGE);
return;
}
String msg = "姓名:"+userName+",性别:"+sex+",爱好:"+interest+
",最高学历:"+degrees;
JOptionPane.showMessageDialog(null, msg, "ᨀ示",
JOptionPane.INFORMATION_MESSAGE);
}
}
定义一个工厂类,获得具体服务层的对象
分层开发实现注册功能—服务层之工厂类
package com.gui;
/**
* 用户模块服务层工厂类.
*/
public abstract class UserServiceFactory {
/**
* 获得注册功能业务逻辑对象.
*/
public static UserService createRegistService() {
return new RegistService();
}
}
然后,定义 1 个监听类(对应注册事件监听)
分层开发实现注册功能—监听层之注册按钮监听类
package com.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 注册功能监听类.
*/
public class RegistUserFrame_btnRegist_ActionListener implements ActionListener {
private RegistUserFrame ruf;
private UserService service;
public RegistUserFrame_btnRegist_ActionListener(RegistUserFrame ruf) {
this.ruf = ruf;
service = UserServiceFactory.createRegistService();
}
@Override
public void actionPerformed(ActionEvent e) {
service.execute(e, ruf);
}
}
最后,定义视图层
分层开发实现注册功能—视图层之窗口类
package com.gui;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/**
* 用户注册窗口类.
*/
public class RegistUserFrame extends JFrame {
private JPanel pnlMain;
private JLabel lblTitle;
private JLabel lblUserName;
private JLabel lblUserPwd;
private JLabel lblUserSex;
private JLabel lblInterest;
private JLabel lblDegrees;
private JTextField txtUserName;
private JPasswordField pwdUserPwd;
private JRadioButton rabM;
private JRadioButton rabF;
private ButtonGroup btgSex;
private JCheckBox chkTV;
private JCheckBox chkGame;
private JCheckBox chkStudy;
private JCheckBox chkOther;
private JComboBox<String> cmbDegrees;
private DefaultComboBoxModel dcmDegrees;
private JButton btnRegist;
public RegistUserFrame() {
pnlMain = new JPanel(null);
lblTitle = new JLabel("用户注册");
lblUserName = new JLabel("用户姓名:");
lblUserPwd = new JLabel("用户密码:");
lblUserSex = new JLabel("用户性别:");
lblInterest = new JLabel("兴趣爱好:");
lblDegrees = new JLabel("最高学历:");
txtUserName = new JTextField();
pwdUserPwd = new JPasswordField();
rabM = new JRadioButton("男");
rabF = new JRadioButton("女");
chkGame = new JCheckBox("看电视");
chkStudy = new JCheckBox("学习");
chkGame = new JCheckBox("玩游戏");
chkOther = new JCheckBox("其他");
cmbDegrees = new JComboBox<>();
dcmDegrees = new DefaultComboBoxModel();
btnRegist = new JButton("注册");
init();
}
// 为控件定义相应的get方法
public JTextField getTxtUserName() {
return txtUserName;
}
public JPasswordField getPwdUserPwd() {
return pwdUserPwd;
}
public JRadioButton getRabM() {
return rabM;
}
public JRadioButton getRabF() {
return rabF;
}
public JCheckBox getChkTV() {
return chkTV;
}
public JCheckBox getChkGame() {
return chkGame;
}
public JCheckBox getChkStudy() {
return chkStudy;
}
public JCheckBox getChkOther() {
return chkOther;
}
public JComboBox<String> getCmbDegrees() {
return cmbDegrees;
}
public DefaultComboBoxModel getDcmDegrees() {
return dcmDegrees;
}
private void init() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("注册界面");
this.setSize(400, 370);
//设置各个控件的坐标和位置
lblTitle.setBounds(150, 10, 100, 30);
lblUserName.setBounds(20, 60, 80, 25);
lblUserPwd.setBounds(20, 100, 80, 25);
lblUserSex.setBounds(20, 140, 80, 25);
lblInterest.setBounds(20, 180, 80, 25);
lblDegrees.setBounds(20, 220, 80, 25);
txtUserName.setBounds(110, 60, 120, 25);
pwdUserPwd.setBounds(110, 100, 120, 25);
rabM.setBounds(110, 140, 50, 25);
rabF.setBounds(160, 140, 50, 25);
chkTV.setBounds(110, 180, 70, 25);
chkGame.setBounds(180, 180, 70, 25);
chkStudy.setBounds(250, 180, 70, 25);
chkOther.setBounds(320, 180, 70, 25);
cmbDegrees.setBounds(110, 220, 80, 25);
btnRegist.setBounds(150,270,80,25);
//将性别的 2 个单选按钮设置为同一组
btgSex.add(rabM);
btgSex.add(rabF);
//让“男”的单选按钮默认为选中
rabM.setSelected(true);
setCmbDegreesData();
//设置下拉列表中的第 3 项为默认选中
this.cmbDegrees.setSelectedIndex(2);
/*
* 在注册按钮上添加按钮按下监听对象,
* 并在实例化监听对象中传入当前窗口对象本身
*/
btnRegist.addActionListener(
new RegistUserFrame_btnRegist_ActionListener(this));
pnlMain.add(lblTitle);
pnlMain.add(lblUserName);
pnlMain.add(lblUserPwd);
pnlMain.add(lblUserSex);
pnlMain.add(lblInterest);
pnlMain.add(lblDegrees);
pnlMain.add(txtUserName);
pnlMain.add(pwdUserPwd);
pnlMain.add(rabM);
pnlMain.add(rabF);
pnlMain.add(chkTV);
pnlMain.add(chkGame);
pnlMain.add(chkStudy);
pnlMain.add(chkOther);
pnlMain.add(cmbDegrees);
pnlMain.add(btnRegist);
this.add(pnlMain);
this.setVisible(true);
}
private void setCmbDegreesData() {
//向 Model 中追加选项
dcmDegrees.addElement("博士");
dcmDegrees.addElement("硕士");
dcmDegrees.addElement("学士");
dcmDegrees.addElement("专科");
dcmDegrees.addElement("其他");
//在下拉列表上绑定 Model
cmbDegrees.setModel(dcmDegrees);
}
}