用户管理系统功能简介
本篇将介绍一个模拟的用户管理系统。用户能够将自己的账号、密码、姓名、部门存入数据库(为了简单起见,这里用文件来代替)。
该系统由四个界面组成。
1.登录界面
系统运行,出现登录界面,如图所示。
该界面出现在屏幕中间,在这个界面中:
(1)单击“登录”按钮,能够根据输入的账号密码进行登录;如果登录失败,能够提示;如果登录成功,提示登录成功之后,能到达操作界面。
(2)单击“注册”按钮,登录界面消失,出现注册界面。
(3)单击“退出”按钮,程序退出。
2.注册界面
注册界面如图所示。
在这个界面中:
(1)单击“注册”按钮,能够根据输入的账号、密码、姓名、部门进行注册。两个密码必须相等,账号不能重复注册,部门选项如图所示。
(2)单击“登录”按钮,注册界面消失,出现登录界面。
(3)单击“退出”按钮,程序退出。
3.操作界面
用户登录成功之后,出现操作界面,该界面效果如图所示。
在这个界面中:
(1)标题栏显示当前登录的账号。
(2)单击“显示详细信息”按钮,显示用户的详细信息。如图所示。
(3)单击“退出”按钮,程序退出。
(4)单击“修改个人资料”按钮,显示修改个人资料的界面。
4.修改个人资料界面
该界面如图所示。
所有内容均初始化填入相应的控件。账号不可修改。
在这个界面中:
(1)单击“修改”按钮,能够修改用户信息。
(2)单击“关闭”按钮,能够关掉该界面。
关键技术
1.如何组织界面
在这个项目中,需要用到一下几个界面:登录界面,注册界面,操作界面和修改界面。很明显,这些界面各自有自己的控件和事件,4个界面应该分4个类,在各个类里面负责界面的界面元素和事件处理,是比较好的方法。
设计出来的类如下:
1.LoginFrame:登录界面。
2.ResgisterFrame:注册界面。
3.OperationFrame:操作界面。
4.ModifyFrame:修改界面。
2.如何访问文件
该项目在几个界面类中都用到了文件操作,如果将文件操作的代码分散在多个界面类中,维护性较差。因此,有必要将文件操作的代码专门放在一个类中,让各个界面调用。
数据保存在“cus.inc”中,以“账号=密码#姓名#部门”的格式保存,便于Properties类来读。读文件类为FileOpe。负责读文件,将信息保存到文件中。
3.如何保持状态
将项目划分为几个模块之后,模块之间的数据传递难度增大了。比如,在登录界面中,登录成功之后,系统就应该记住该用户的所有信息;否则到了操作界面,无法知道是谁在登录,到了修改界面,更无法显示其详细信息。
有很多方法可以保存其状态。这里采用“静态变量法”。该方法就是将各个模块之间需要共享的数据保存在其某给类的静态变量中。静态变量一旦赋值,在另一个时刻访问,仍然是这个值,因此,可以用静态变量来传送数据。
设计的类如下:
Conf:内含4个静态成员。
1.public static String account; //保存登录用户的账号。
2.public static String password; //保存登录用户的密码。
3.public static String name; //保存登录用户的姓名。
4.public static String dept; //保存登录用户的部门。
4.其他公共功能
在本项目中,界面都要显示在屏幕中间,因此,可以编写一段公用代码完成这个功能。该公用代码放在GUIUtil类中。
ps:还有一些资源文件事先要建好。比如,登录界面上的欢迎图片,数据文件cus.inc等。
代码编写
1.首先是Conf类,比较简单:
package UserManageSystem;public class Conf { public static String account; public static String password; public static String name; public static String dept;}123456789
2.然后是FileOpe类:
package UserManageSystem;import java.io.Filereader;import java.io.PrintStream;import java.util.Properties;import javax.swing.JOptionPane;public class FileOpe { private static String fileName=\”cus.inc\”; private static Properties pps; static { pps=new Properties(); FileReader reader=null; try { reader=new FileReader(fileName); pps.load(reader); } catch (Exception ex) { JOptionPane.showmessageDialog(null,\”文件操作异常\”); System.exit(0); } finally { try { reader.close(); } catch (Exception ex) {} } } private static void listInfo() { PrintStream ps=null; try { ps=new PrintStream(fileName); pps.list(ps); } catch (Exception ex) { JOptionPane.showMessageDialog(null,\”文件操作异常\”); System.exit(0); } finally { try { ps.close(); } catch (Exception ex) {} } } public static void getInfoByaccount(String account) { String cusInfo=pps.getProperty(account); if(cusInfo!=null) { String[] infos=cusInfo.split(\”#\”); Conf.account=account; Conf.password=infos[0]; Conf.name=infos[1]; Conf.dept=infos[2]; } } public static void updateCustomer(String account,String password, String name,String dept) { pps.setProperty(account,password \”#\” name \”#\” dept); listInfo(); }}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
ps:本类中,静态代码块中的代码负责载入cus.inc中的数据
3.接下来是GUIUtil类:
package UserManageSystem;import java.awt.Component;import java.awt.GraphicsEnvironment;import java.awt.Rectangle;public class GUIUtil { public static void toCenter(Component comp) { GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); Rectangle rec=ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds(); comp.setLocation(((int)(rec.getWidth()-comp.getWidth())/2), ((int)(rec.getHeight()-comp.getHeight()))/2); }}123456789101112131415
ps:本类中,toCenter(Component comp)函数传入的参数不是JFrame,而是其弗雷Component,完全是为了扩大本函数的适用范围,让其适用于所有Component子类。
4.登录界面类:
package UserManageSystem;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Icon;import javax.swing.JOptionPane;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JButton;import javax.swing.JTextField;public class LoginFrame extends JFrame implements ActionListener { /****************************定义各控件**************************/ private Icon welcomeIcon=new ImageIcon(\”welcome.png\”); private JLabel lbWelcome=new JLabel(welcomeIcon); private JLabel lbAccount=new JLabel(\”请您输入账号\”); private JTextField tfAccount=new JTextField(10); private JLabel lbPassword=new JLabel(\”请您输入密码\”); private JPasswordField pfPassword=new JPasswordField(10); private JButton btLogin=new JButton(\”登录\”); private JButton btRegister=new JButton(\”注册\”); private JButton btExit=new JButton(\”退出\”); public LoginFrame() { /**********************界面初始化*****************************/ super(\”登录\”); this.setLayout(new FlowLayout()); this.add(lbWelcome); this.add(lbAccount); this.add(tfAccount); this.add(lbPassword); this.add(pfPassword); this.add(btLogin); this.add(btRegister); this.add(btExit); this.setSize(240,180); GUIUtil.toCenter(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); /*****************************增加监听************************/ btLogin.addActionListener(this); btRegister.addActionListener(this); btExit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==btLogin) { String account=tfAccount.getText(); String password=new String(pfPassword.getPassword()); FileOpe.getInfoByAccount(account); if(Conf.account==null||!Conf.password.equals(password)) { JOptionPane.showMessageDialog(this,\”登录失败\”); return; } JOptionPane.showMessageDialog(this,\”登录成功\”); this.dispose(); new OperationFrame(); } else if(e.getSource()==btRegister) { this.dispose(); new RegisterFrame(); } else { JOptionPane.showMessageDialog(this,\”谢谢光临\”); System.exit(0); } }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
ps:本类中this.dispose();表示让本界面消失,释放内存,但是程序不结束。System.exit(0);表示整个程序退出。
5.注册界面类:
package UserManageSystem;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;public class RegisterFrame extends JFrame implements ActionListener { /**********************定义各控件********************************/ private JLabel lbAccount=new JLabel(\”请您输入账号\”); private JTextField tfAccount=new JTextField(10); private JLabel lbPassword=new JLabel(\”请您输入密码\”); private JPasswordField pfPassword=new JPasswordField(10); private JLabel lbPassword2=new JLabel(\”输入确认密码\”); private JPasswordField pfPassword2=new JPasswordField(10); private JLabel lbName=new JLabel(\”请您输入姓名\”); private JTextField tfName=new JTextField(10); private JLabel lbDept=new JLabel(\”请您选择部门\”); private JComboBox cbDept=new JComboBox(); private JButton btRegister=new JButton(\”注册\”); private JButton btLogin=new JButton(\”登录\”); private JButton btExit=new JButton(\”退出\”); public RegisterFrame() { /******************界面初始化********************/ super(\”注册\”); this.setLayout(new FlowLayout()); this.add(lbAccount); this.add(tfAccount); this.add(lbPassword); this.add(pfPassword); this.add(lbPassword2); this.add(pfPassword2); this.add(lbName); this.add(tfName); this.add(lbDept); this.add(cbDept); cbDept.addItem(\”财务部\”); cbDept.addItem(\”行政部\”); cbDept.addItem(\”客服服务部\”); cbDept.addItem(\”销售部\”); this.add(btRegister); this.add(btLogin); this.add(btExit); this.setSize(240,220); GUIUtil.toCenter(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); /****************************增加监听***********************/ btLogin.addActionListener(this); btRegister.addActionListener(this); btExit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==btRegister) { String password1=new String(pfPassword.getPassword()); String password2=new String(pfPassword2.getPassword()); if(!password1.equals(password2)) { JOptionPane.showMessageDialog(this,\”两个密码不相同\”); return; } String account=tfAccount.getText(); FileOpe.getInfoByAccount(account); if(Conf.account!=null) { JOptionPane.showMessageDialog(this,\”用户已经注册\”); return; } String name=tfName.getText(); String dept=(String)cbDept.getSelectedItem(); FileOpe.updateCustomer(account,password1,name,dept); JOptionPane.showMessageDialog(this,\”注册成功\”); } else if(e.getSource()==btLogin) { this.dispose(); new LoginFrame(); } else { JOptionPane.showMessageDialog(this,\”谢谢光临\”); System.exit(0); } }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
6.操作界面类:
package UserManageSystem;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JOptionPane;public class OperationFrame extends JFrame implements ActionListener { /************************定义各控件******************************/ private String welcomMsg=\”选择如下操作:\”; private JLabel lblWelcome=new JLabel(welcomMsg); private JButton btQuery=new JButton(\”显示详细信息\”); private JButton btModify=new JButton(\”修改个人资料\”); private JButton btExit=new JButton(\”退出\”); public OperationFrame() { /****************************界面初始化************************/ super(\”当前登录: \” Conf.account); this.setLayout(new GridLayout(4,1)); this.add(lblWelcome); this.add(btQuery); this.add(btModify); this.add(btExit); this.setSize(300,250); GUIUtil.toCenter(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setVisible(true); /**********************增加监听*******************************/ btQuery.addActionListener(this); btModify.addActionListener(this); btExit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==btQuery) { String message=\”您的详细资料为:n\”; message =\”账号:\” Conf.account \”n\”; message =\”姓名:\” Conf.name \”n\”; message =\”部门:\” Conf.dept \”n\”; JOptionPane.showMessageDialog(this,message); } else if(e.getSource()==btModify) { new ModifyDialog(this); } else { JOptionPane.showMessageDialog(this,\”谢谢光临\”); System.exit(0); } }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
7.最后是ModifyDialog类,注意ModifyDialog是一个模态对话框。
package UserManageSystem;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JDialog;import javax.swing.JOptionPane;import javax.swing.JPasswordField;import javax.swing.JTextField;public class ModifyDialog extends JDialog implements ActionListener { /*******************************定义各控件**************************/ private JLabel lbMsg=new JLabel(\”您的账号为:\”); private JLabel lbAccount=new JLabel(Conf.account); private JLabel lbPassword=new JLabel(\”请您输入密码\”); private JPasswordField pfPassword=new JPasswordField(Conf.password,10); private JLabel lbPassword2=new JLabel(\”输入确认密码\”); private JPasswordField pfPassword2=new JPasswordField(Conf.password,10); private JLabel lbName=new JLabel(\”请您修改姓名\”); private JTextField tfName=new JTextField(Conf.name,10); private JLabel lbDept=new JLabel(\”请您修改部门\”); private JComboBox cbDept=new JComboBox(); private JButton btModify=new JButton(\”修改\”); private JButton btExit=new JButton(\”关闭\”); public ModifyDialog(JFrame frm) { /***********************界面初始化***************************/ super(frm,true); this.setLayout(new GridLayout(6,2)); this.add(lbMsg); this.add(lbAccount); this.add(lbPassword); this.add(pfPassword); this.add(lbPassword2); this.add(pfPassword2); this.add(lbName); this.add(tfName); this.add(lbDept); this.add(cbDept); cbDept.addItem(\”财务部\”); cbDept.addItem(\”行政部\”); cbDept.addItem(\”客户服务部\”); cbDept.addItem(\”销售部\”); cbDept.setSelectedItem(Conf.dept); this.add(btModify); this.add(btExit); this.setSize(240,200); GUIUtil.toCenter(this); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); /*******************增加监听*******************************/ btModify.addActionListener(this); btExit.addActionListener(this); this.setResizable(false); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==btModify) { String password1=new String(pfPassword.getPassword()); String password2=new String(pfPassword2.getPassword()); if(!password1.equals(password2)) { JOptionPane.showMessageDialog(this,\”两个密码不相同\”); return; } String name=tfName.getText(); String dept=(String)cbDept.getSelectedItem(); //将新的值存入静态变量 Conf.password=password1; Conf.name=name; Conf.dept=dept; FileOpe.updateCustomer(Conf.account,password1,name,dept); JOptionPane.showMessageDialog(this,\”修改成功\”); } else { this.dispose(); } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
8.主函数类,用来调用登录界面测试程序:
package UserManageSystem;public class Main { public static void main(String[] args) { new LoginFrame(); }}12345678
运行该类,则可以出现登录界面。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。