A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


下面是我的代码 可能有点乱  网上找了好几种方法,但是就是找不到具体的原理,所以一下子都搬来了- -
package cn.itcast.AbstractFrame;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.itcast.Util.Util;

public abstract class AbstractLoginFrame extends JFrame {
        protected JTextField txtUserName = new JTextField();//登录名文本框
        protected JPasswordField txtPwd = new JPasswordField();//密码框
       
       
        public AbstractLoginFrame(){
                this.init();
                this.addComponent();
                Util.setFrameImage(this);
        }
        private void init() {
                //设置标题
                this.setTitle("清华大学登录系统");
                //设置大小和位置
                this.setBounds(550, 200, 500, 400);
                //设置默认的关闭行为
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                //设置布局管理器
                this.setLayout(null);
                Util.setFrameImage(this);
        }
        private void addComponent() {
               
        /*        JLayeredPane layers = new JLayeredPane();//创建
                JPanel paneBackground = new JPanel(new BorderLayout());//背景层
                paneBackground.add(new JLabel(Toolkit.getDefaultToolkit().getImage("E:/1.jpg")), BorderLayout.CENTER);//加入背景图片,BorderLayout.CENTER单独使用表示填满。
                JPanel paneDetails = new JPanel(new FlowLayout());//这层你往里面加东西,就在背景上面了。
                paneDetails.add(new JButton("ABC"));//加个按钮试试。
                layers.add(paneBackground);layers.add(paneDetails);//把层按照从背后向前的顺序加进去。
                fr.add(layers, BorderLayout.CENTER);//把layers加进frame里面去,填满。
*/               
               
                //1.标题标签:
                JLabel lb1 =  new JLabel(" 腾讯QQ");
                lb1.setBounds(180,30, 200, 50);
                lb1.setFont(new Font("宋体",Font.BOLD,28));
                this.add(lb1);
                //2.用户名:标签
                JLabel lb2 = new JLabel("登录名:");
                lb2.setBounds(50, 100, 100, 30);
                lb2.setFont(new Font("宋体",Font.BOLD,18));
                this.add(lb2);
                //3.用户名:文本框--JTextField
               
                txtUserName.setBounds(150, 100, 250, 30);
                this.add(txtUserName);
                //4.密码:标签
                JLabel lb3 = new JLabel("密  码:");
                lb3.setBounds(50, 180, 100, 30);
                lb3.setFont(new Font("宋体",Font.BOLD,18));
                this.add(lb3);
                //5.密码:文本框--JPasswordFiled
               
                txtPwd.setBounds(150, 180, 250, 30);
                this.add(txtPwd);
               
                //6.关闭:按钮
                JButton butClose = new JButton("关  闭");
                butClose.setBounds(30, 270, 100, 25);
                butClose.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                //调用外部方法
                                butClose();
                        }

                        });
                this.add(butClose);
                //7.注册:按钮
                JButton butRegist = new JButton("注  册");
                butRegist.setBounds(180, 270, 100, 25);
                butRegist.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                butRegist();
                        }});
               
                this.add(butRegist);
                //8.登陆:按钮
                JButton butOK = new JButton("登  录");
                butOK.setBounds(320, 270, 100, 25);
                butOK.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                butOK();
                        }});
                this.add(butOK);
               
               
        }
        protected abstract void butRegist();
        //登陆按钮
        protected abstract void butOK();


        //关闭按钮
        private  void butClose() {
                System.exit(0);
        }
}







package cn.itcast.AbstractFrame;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.itcast.Util.Util;

        public abstract class AbstractRegistFrame extends JFrame {
                //为了其它成员能访问到,也为了子类能访问到,所以这些组件被定义在"成员变量"的位置
                protected JTextField txtUserName = new JTextField();//用户名文本框
                protected JPasswordField txtPwd = new JPasswordField();//密码框
                protected JPasswordField txtPwd2 = new JPasswordField();//确认密码
               
               
                public AbstractRegistFrame(){
                        this.init();
                        this.addComponent();
                }
                private void init() {
                        //设置标题
                        this.setTitle("注册新用户");
                        //设置大小和位置
                        this.setBounds(550, 200, 500, 400);
                        //设置默认的关闭行为
                        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        //设置布局管理器
                        this.setLayout(null);       
                        Util.setFrameImage(this);
                }
                private void addComponent() {
                        //1.标题标签:
                        JLabel lb1 =  new JLabel("注册新用户");
                        lb1.setBounds(180,30, 200, 50);
                        lb1.setFont(new Font("宋体",Font.BOLD,28));
                        this.add(lb1);
                        //2.用户名:标签
                        JLabel lb2 = new JLabel("用 户 名:");
                        lb2.setBounds(50, 100, 100, 30);
                        lb2.setFont(new Font("宋体",Font.BOLD,18));
                        this.add(lb2);
                        //3.用户名:文本框--JTextField
                       
                        txtUserName.setBounds(150, 100, 250, 30);
                        this.add(txtUserName);
                        //4.密码:标签
                        JLabel lb3 = new JLabel("密    码:");
                        lb3.setBounds(50, 150, 100, 30);
                        lb3.setFont(new Font("宋体",Font.BOLD,18));
                        this.add(lb3);
                        //5.密码:文本框--JPasswordFiled
                       
                        txtPwd.setBounds(150, 150, 250, 30);
                        this.add(txtPwd);
                       
                        //6.确认密码:标签
                        JLabel lb4 = new JLabel("确认密码:");
                        lb4.setBounds(50,200,100,30);
                        lb4.setFont(new Font("宋体",Font.BOLD,18));
                        this.add(lb4);
                        //7.确认密码:文本框
                       
                        txtPwd2.setBounds(150, 200, 250, 30);
                        this.add(txtPwd2);
                       
                        //6.关闭:按钮
                        JButton butClose = new JButton("关  闭");
                        butClose.setBounds(50, 270, 100, 25);
                        butClose.addActionListener(new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                        //调用外部方法
                                        butClose();
                                }

                                });
                        this.add(butClose);
                       
                        //8.登陆:按钮
                        JButton butOK = new JButton("确  定");
                        butOK.setBounds(300, 270, 100, 25);
                        butOK.addActionListener(new ActionListener(){
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                        butOK();
                                }});
                        this.add(butOK);
                       
                       
                }
                //登陆按钮
                protected abstract void butOK();


                //关闭按钮
                protected abstract  void butClose();
               
       
        }






package cn.itcast.Frame;

import javax.swing.JOptionPane;

import cn.itcast.AbstractFrame.AbstractLoginFrame;
import cn.itcast.MainAPP.MainAPP;
import cn.itcast.Uesr.User;
import cn.itcast.Util.Util;

public class LoginFrame extends AbstractLoginFrame{
          int count = 3;
         
          
          protected void butOK(){
                  Util.setFrameImage(this);
                String uName = this.txtUserName.getText();
                String pwd = new String(this.txtPwd.getPassword());
                //验证非空
                if (uName.trim().length()==0){
                        JOptionPane.showMessageDialog(this, "请输入用户名");
                        return;
                }
                if(pwd.trim().length()==0){
                        JOptionPane.showMessageDialog(this, "请输入密码");
                }
                count--;
                for(int i=0;i<MainAPP.userList.size();i++){
                        User user=MainAPP.userList.get(i);
                        if(user.getLoginname().equals(uName)&&user.getLonginpwd().equals(pwd)){
                                JOptionPane.showMessageDialog(this, "欢迎"+uName+"同学驾到,吾皇万岁万岁万万岁!");
                                return;
                        }
                }       
               
                if(count>0){
                        JOptionPane.showMessageDialog(this, "账号或密码错误,请重新输入!你还有"+count+"次机会!");
                        return;
                }else{
                        JOptionPane.showMessageDialog(this, "很遗憾,您输入错误超过三次,系统退出,请重新做人!");
                        JOptionPane.showMessageDialog(this,"呵呵");
                        System.exit(0);
                }       
        }
        protected void butRegist(){
                new RegistFrame().setVisible(true);
                this.dispose();
        }
}






package cn.itcast.Frame;
import javax.swing.JOptionPane;

import cn.itcast.AbstractFrame.AbstractRegistFrame;
import cn.itcast.MainAPP.MainAPP;
import cn.itcast.Uesr.User;
import cn.itcast.Util.Util;

public class RegistFrame extends AbstractRegistFrame {
       
        protected void butOK(){
                //获取输入的三组字符串
                Util.setFrameImage(this);//给窗体设置Util里面的图片
                String uName = this.txtUserName.getText();
                String pwd1 = new String(this.txtPwd.getPassword());
                String pwd2 = new String(this.txtPwd2.getPassword());
                //判断非空
                if(uName.trim().length()==0){
                        JOptionPane.showMessageDialog(this, "请输入用户名");
                        return;
                }
                if(pwd1.trim().length()==0){
                        JOptionPane.showMessageDialog(this, "请输入密码");
                }
                if(pwd2.trim().length()==0){
                        JOptionPane.showMessageDialog(this, "请确认密码");
                }
                //确认用户名
                for(int i=0;i<MainAPP.userList.size();i++){
                        User user=MainAPP.userList.get(i);
                        if(user.getLoginname().equals(uName)){
                                JOptionPane.showMessageDialog(this, "该用户名已经存在,请重新输入");
                                return;
                        }
                }
                if(!pwd1.equals(pwd2)){
                        JOptionPane.showMessageDialog(this, "两次密码不相同,请重新输入!");
                        return;
                }
               
                User user = new User(uName,pwd1);
                MainAPP.userList.add(user);
                JOptionPane.showMessageDialog(this, "注册成功!");
                new LoginFrame().setVisible(true);
                this.dispose();
               
               
        }
       
        protected void butClose(){
                new LoginFrame().setVisible(true);
                this.dispose();
        }
       
}






package cn.itcast.MainAPP;

import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import cn.itcast.Frame.LoginFrame;
import cn.itcast.Uesr.User;
import cn.itcast.resource.MyLookAndFeel;

/*
*思路:
*        main方法开始,创建一个窗口,设置好指定参数。制作成方法
*        再向窗口中添加组件,通过addComponent方法添加组件。然后通过事件监听向其中给登录和取消已经注册完成对应功能。
*                其中登录和注册方法取抽象方法,以便形成跳转。比如单击取消,销毁自己。然后下一个窗口可见。
*        再向注册页面添加组件,方法同窗口中添加组件。然后再调用那三个抽象方法已实现跳转功能
*                创建一个static静态集合已方便调用,然后通过文本框和密码框向其中输入数据,通过某个引用之间来进行传递。
*        对应的,需要创建一个用户目录,以便储存集合中的所需的元素和调用,成员变量采用私有成员变量,以便符合java bean
*        的需求。
*                优化:向集合中填写注册信息,注册信息是通过注册页面(继承自抽象登录页面)以便往里添加信息,这其中可以添加
*一些小功能以提升程序的娱乐性(如输入密码的最大次数等)。然后再通过登录页面来实现。
*                主方法开始窗口,设置可见,开始运行。
* */
public class MainAPP {

        // 创建集合,用于储存用户信息
        public static ArrayList<User> userList = new ArrayList<>();
       
        Image image = new ImageIcon("src\\cn\\itcast\\resource\\1.jpg", "啦啦啦啦啦啦啊").getImage();
        public void paint(Graphics g){
                super.paint(g);
                g.drawImage(image, 0,0, null);
        }
       
        public static void main(String[] args) {
               
               
                try {
                        UIManager.setLookAndFeel(MyLookAndFeel.JTATTOO_AERO);
                } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

                new LoginFrame().setVisible(true);
        }
       
}
//背景更改方式1
        /*Image image = new ImageIcon("src\\cn\\itcast\\resource\\1.jpg").getImage();

        // ////
        // 覆盖JFrame的 paint方法

        public void paint(Graphics g) {
                super.paint(g);
                g.drawImage(image, 0, 0, null);

        }*/
//背景更改方式2

/*
* class CInstead extends JPanel { ImageIcon icon; Image img;
*
* public CInstead() { icon = new
* ImageIcon(LoginFrame.class.getResource("src\\cn\\itcast\\resource\\1.jpg"));
* img = icon.getImage(); } public void paintComponent(Graphics g) {
* super.paintComponent(g); g.drawImage(img,0,0,null ); }
*
* }
*/






package cn.itcast.Uesr;

public class User {
        private String loginname;
        private String longinpwd;
        //全参构造
        public User(String loginname, String longinpwd) {
                super();
                this.loginname = loginname;
                this.longinpwd = longinpwd;
        }
        //toString方法
        @Override
        public String toString() {
                return "User [loginname=" + loginname + ", longinpwd=" + longinpwd
                                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
                                + ", toString()=" + super.toString() + "]";
        }
        //无参构造
        public User() {
                super();
        }
        //对应get/set方法
        public String getLoginname() {
                return loginname;
        }
        public void setLoginname(String loginname) {
                this.loginname = loginname;
        }
        public String getLonginpwd() {
                return longinpwd;
        }
        public void setLonginpwd(String longinpwd) {
                this.longinpwd = longinpwd;
        }
       
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马