java实现登陆窗口,java实现用户登录界面
本文实例为大家分享了爪哇岛实现登录窗口的具体代码,供大家参考,具体内容如下
登录窗口主类
包ccnu.paint导入Java。awt。颜色;导入Java。awt。字体;导入Java。awt。gridlayout导入Java。awt。点;导入Java。awt。事件。动作事件;导入Java。awt。事件。动作监听器;导入Java。awt。事件。鼠标适配器;导入Java。awt。事件。鼠标事件;导入Java。io。文件;导入Java。io。文件输入流;导入Java。io。io异常;导入Java。util。枚举;导入Java。util。属性;导入javax。挥棒。图像图标;导入javax。挥棒。jbutton导入javax。挥棒。jframe导入javax。挥棒。jlabel导入javax。挥棒。joptionpane导入javax。挥棒。jpanel导入javax。挥棒。jpasswordfield导入javax。挥棒。jtextfield导入ccnu。util。回答;导入ccnu。util。验证;公共类登录扩展JFrame { private static final long serial version uid=1L;私有属性pro=new Properties();私有布尔ver _ code=false//默认输入验证码错误私答答案=空私有JPanel P1=new JPanel();//添加到面板中的组件默认为流式布局private JLabel luser=new JLabel(用户名: );二等兵JTextField用户名=new JTextField(20);私有JPanel p2=new JPanel();private JLabel lpwd=new JLabel( password : );private JPasswordField pwd=new JPasswordField(20);私有JPanel P4=new JPanel();private JLabel lVer=new JLabel(验证: );private JTextField ver=new JTextField(10);private JLabel img=new JLabel();private JLabel result=new JLabel();私有JPanel P3=new JPanel();private JButton ok=new JButton( ok );private JButton cancel=new JButton( cancel );private JButton Sign up=new JButton( Sign up );//用于账户注册//设置组件的监听公共void初始化侦听器(){用户名。addactionlistener(new action listener(){ @ Override public void action performed(action event e){//JTextField的行为是回车键字符串名称=用户名。gettext();//登录。这个。settitle(名称);//系统。出去。println(名称。hashcode()“* * *”.hashCode());如果(姓名。equals(){ joptionpane。showmessagedialog(登录。“这,”请输入用户名!);}
else { pwd.grabFocus(); } } }); pwd.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String password = new String(pwd.getPassword()); if(password.equalsIgnoreCase("")) { JOptionPane.showMessageDialog(Login.this, "please input a password!"); }else{ ver.grabFocus(); } } }); ok.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // 重新加载最新的账户文件 try { pro.load(new FileInputStream(new File("src/res/accouts.properties"))); } catch (IOException e1) { e1.printStackTrace(); } check(); } }); // 判断验证码是否正确 ver.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { String verCode = ver.getText(); if(verCode.equals("")) { JOptionPane.showMessageDialog(Login.this, "Please input a verification!"); }else{ if(verCode.equals(answer.getResult())) { result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg"))); // 显示提示的图片信息(如√图片) ver_code = true; // 检查之前,重新加载最新的账户文件 try { pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 将账户文件加载进来 } catch (IOException e1) { e1.printStackTrace(); } check(); }else{ result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg"))); // 显示提示的图片信息(如×图片) ver_code = false; } } } }); // 点击图片会更改验证码 img.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e) { answer = Verification.verification(); img.setIcon(new ImageIcon(answer.getBufferedImage())); // 设置验证码图案 } }); cancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int option = JOptionPane.showConfirmDialog(Login.this, "Are you sure to exit?");// System.out.println("option = " + option); if (option == 0) {// Yes Login.this.dispose(); } } }); signUp.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { new SignUp(); } }); } // 初始化登录窗口及其组件的设置监听 public Login() { super("Login"); // 加载账户文件 try { pro.load(new FileInputStream(new File("src/res/accouts.properties"))); // 从指定位置将账户文件加载进来 } catch (IOException e) { e.printStackTrace(); } initListener(); answer = Verification.verification(); // 生成验证码 img.setIcon(new ImageIcon(answer.getBufferedImage())); // 设置初始验证码 this.setLocation(new Point(200, 200)); this.setSize(500, 300); this.setLayout(new GridLayout(4, 1, 0, 20)); // 垂直间隙为20px p1.add(luser); p1.add(username); p2.add(lpwd); p2.add(pwd); p4.add(this.lVer); p4.add(this.ver); p4.add(this.img); result.setForeground(Color.red); result.setFont(new Font("楷体", Font.BOLD, 20)); p4.add(result); p3.add(ok); p3.add(cancel); p3.add(signUp); this.add(p1); this.add(p2); this.add(p4); this.add(p3);// this.setBackground(Color.blue); // JFrame的上层还有一个ContentPane this.getContentPane().setBackground(Color.gray); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 等价于Frame中的windowClosing事件 } // 检查用户名或密码 public void check() { String verCode = ver.getText(); if(verCode.equals("")) { JOptionPane.showMessageDialog(Login.this, "Please input a verification!"); return; }else{ if(verCode.equals(answer.getResult())) { result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/right.jpg"))); ver_code = true; }else{ result.setIcon(new ImageIcon(Login.this.getClass().getResource("/res/error.jpg"))); ver_code = false; } } if(ver_code == false) { JOptionPane.showMessageDialog(this, "verification is error!"); return; } String name = username.getText(); String password = new String(pwd.getPassword()); // return char[]// if (name.equalsIgnoreCase("admin") && password.equals("123456")) if (isPass(name, password)) {// new PaintApp(name); JOptionPane.showMessageDialog(this, "-^_^- OK..."); // 此处可以加上其他的登陆成功后进一步处理的窗口 this.dispose(); } else { JOptionPane.showMessageDialog(this, "userName or password is incorrect!"); username.setText(""); pwd.setText(""); ver.setText(""); answer = Verification.verification(); img.setIcon(new ImageIcon(answer.getBufferedImage())); result.setIcon(null); } } // 验证用户输入的账户名和密码是否正确(通过与加载进来的账户 pro 比对) public boolean isPass(String name, String password) { Enumeration en = pro.propertyNames(); while(en.hasMoreElements()) { String curName = (String)en.nextElement();// System.out.println(curName + "---" + pro.getProperty(curName)); if(curName.equalsIgnoreCase(name)) { if(password.equalsIgnoreCase(pro.getProperty(curName))) { return true; } } } return false; } public static void main(String[] args) { new Login(); }}账户注册类
package ccnu.paint;import java.awt.GridLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.Properties;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class SignUp extends JFrame{ private static final long serialVersionUID = 3054293481122038909L; private Properties pro = new Properties(); // 最好时静态的,因为账户是共享的 private JPanel panel = new JPanel(); private JLabel label = new JLabel("username: "); private JTextField field = new JTextField(15); private JPanel panel2 = new JPanel(); private JLabel label2 = new JLabel("password: "); private JPasswordField field2 = new JPasswordField(15); private JPanel panel3 = new JPanel(); private JLabel label3 = new JLabel("confirmation: "); private JPasswordField field3 = new JPasswordField(15); private JPanel panel4 = new JPanel(); private JButton button = new JButton("OK"); private JButton button2 = new JButton("Cancel"); public void initListener() { field.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { field2.grabFocus(); } }); field2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { field3.grabFocus(); } }); field3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ok_actionPerformed(e); } }); // OK button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { ok_actionPerformed(e); } }); // Cancel button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cancel_actionPerformed(e); } }); } public void ok_actionPerformed(ActionEvent e) { String userName = field.getText(); String password = new String(field2.getPassword()); String password2 = new String(field3.getPassword()); if (userName.equals("")) { JOptionPane.showMessageDialog(SignUp.this, "username cannot be empty!"); } else { if (password.equalsIgnoreCase("")) { JOptionPane.showMessageDialog(SignUp.this, "password cannot be empty!"); } else { if (password2.equalsIgnoreCase(password)) { if (isExist(userName)) { JOptionPane.showMessageDialog(SignUp.this, "username has been existed!"); field.setText(""); field2.setText(""); field3.setText(""); } else { pro.setProperty(userName, password); JOptionPane.showMessageDialo
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。