javax.swing设计的登录界面

特点:没有JFrame自带的边框图标等,所有的容器都看不到JFrame原始样子,支持拖动任意位置

提一嘴:jdk1.7以上可以隐藏JFrame图标任务栏 (jframe.setType(JFrame.Type.UTILITY);),

更多隐藏JFrame图标隐藏学习请点击https://www.freesion.com/article/3886827551/https://www.codenong.com/cs106567490/

 

效果:

 

 

 

代码:

public class LoginView extends JFrame implements MouseListener {

    private static final long serialVersionUID = 1L;

    JLabel jan,bi,tu;//gif,最小化,关闭,logo,QQ,头像
    JLabel sysName;
    JLabel an1,an2,lie1,lie2;//暗色块|线
    JTextField user;//账号
    JPasswordField pass;//密码
    JPanel bgcolor;//白
    JLabel su1,mi1;//缩略图
    JLabel /*gifTxt,*/text3,text4,text5;//动图,自动登录,记住密码,找回密码,注册账号,登录
    static Point origin = new Point();//变量,用于可拖动窗体
    int a=0,b=0,c=0,d=0;//控制线
    JLabel submit;//背景


    public LoginView() {
        setBak(); // 调用背景方法

//实例化
        ImageIcon i = new ImageIcon("src/素材/bggif.gif");
//        gifTxt = new JLabel(i);
        jan = new JLabel(new ImageIcon("src/素材/最小化.png"));
        bi = new JLabel(new ImageIcon("src/素材/关闭.png"));
        sysName = new JLabel("我自己的共享业务智能应用");
        an1 = new JLabel();an2 = new JLabel();//暗调
        tu = new JLabel(new ImageIcon("src/素材/loginicon.png"));
        user = new JTextField();
        pass = new JPasswordField();
        su1 = new JLabel(new ImageIcon("src/素材/头像灰3.png"));
        mi1 = new JLabel(new ImageIcon("src/素材/密码.png"));
        lie1 = new JLabel(new ImageIcon("src/素材/直线2.png"));
        lie2 = new JLabel(new ImageIcon("src/素材/直线2.png"));
        bgcolor = new JPanel();
        text5 = new JLabel("登录");
        submit = new JLabel();

//位置
        jan.setBounds(764, 2, 32, 32);
        bi.setBounds(796, 3, 32, 32);
        sysName.setBounds(490,115,400,60);
        an1.setBounds(761, 0, 35, 35);
        an2.setBounds(795, 0, 35, 35);
//        gifTxt.setBounds(80,80,300,300);
        tu.setBounds(570, 20, 90, 85);
        user.setBounds(530, 200, 180, 40);
        pass.setBounds(530, 240, 180, 40);
        su1.setBounds(500, 210, 20, 20);
        mi1.setBounds(500, 250, 20, 20);
        lie1.setBounds(500, 230, 240, 10);
        lie2.setBounds(500, 270, 240, 10);
        bgcolor.setBounds(470, 65, 320, 400);
        text5.setBounds(606, 325, 80, 20);
        submit.setBounds(500, 320, 242, 35);
//属性
        sysName.setFont(new Font("微软雅黑", 1, 25));
        sysName.setForeground(Color.BLUE);
        an1.setBackground(new Color(0,0,0,0.3f));
        an2.setBackground(new Color(0,0,0,0.3f));
        bgcolor.setBackground(new Color(255, 255, 255));

        user.setForeground(Color.gray);
        user.setText("用户名");
        user.setOpaque(false);//透明背景
        user.setBorder(null);//去掉边框
        user.setFont(new Font("微软雅黑", Font.PLAIN, 16));//框内文字样式
        pass.setFont(new Font("微软雅黑", Font.PLAIN, 16));//框内文字样式
        pass.setBorder(null);//去掉边框

        pass.setOpaque(false);//透明背景
        pass.setForeground(Color.gray);
        pass.setText("密码");
        pass.setEchoChar((char)0);//让密码显示出来

        sysName.setFont(new Font("微软雅黑", 0, 22));
        text5.setFont(new Font("微软雅黑", 0, 15));
        text5.setForeground(Color.white);

        submit.setBackground(new Color(5, 186, 251));
        submit.setOpaque(true);


//事件区域,这么写,可以避免每个地方都监听,用于集中统一处理事件
        jan.addMouseListener(this);
        bi.addMouseListener(this);
        user.addMouseListener(this);
        pass.addMouseListener(this);
        submit.addMouseListener(this);
        this.addMouseListener(this);


        this.addMouseMotionListener(new MouseMotionListener() {//窗体拖动事件
            public void mouseMoved(MouseEvent e) {
            }
            public void mouseDragged(MouseEvent e) {
                Point p = getLocation();
                setLocation(p.x + e.getX() - origin.x, p.y + e.getY()- origin.y);
            }
        });

        user.addFocusListener(new FocusListener() {

            public void focusLost(FocusEvent e) {//失去焦点
                su1.setIcon(new ImageIcon("src/素材/头像灰3.png"));
                lie1.setIcon(new ImageIcon("src/素材/直线2.png"));
                c=0;
                if(user.getText().isEmpty()) {//判断是否为空(为了设置默认提示语)
                    user.setForeground(Color.gray);
                    user.setText("用户名");
                }
            }

            public void focusGained(FocusEvent e) {//得到焦点
                user.setForeground(Color.black);
                lie1.setIcon(new ImageIcon("src/素材/直线3.png"));
                a=1;c=1;b=0;
                su1.setIcon(new ImageIcon("src/素材/头像蓝3.png"));
                if(user.getText().equals("用户名")) {
                    user.setText("");
                }else {
                    user.setText(user.getText());
                    user.selectAll();
                }
            }
        });

        pass.addFocusListener(new FocusListener() {

            public void focusLost(FocusEvent e) {//失去焦点
                lie2.setIcon(new ImageIcon("src/素材/直线2.png"));//失去焦点换图片
                mi1.setIcon(new ImageIcon("src/素材/密码.png"));
                d=0;
                if(pass.getText().isEmpty()) {
                    pass.setForeground(Color.gray);
                    pass.setText("密码");
                    pass.setEchoChar((char)0);//让密码显示出来
                }
            }

            public void focusGained(FocusEvent e) {//得到焦点
                mi1.setIcon(new ImageIcon("src/素材/密码 (1).png"));
                lie2.setIcon(new ImageIcon("src/素材/直线3.png"));
                b=1;a=0;d=1;
                pass.setForeground(Color.black);
                pass.setEchoChar('*');//让用户输入看不见
                if(pass.getText().equals("密码")) {
                    pass.setText("");
                }else {
                    pass.setText(pass.getText());
                }
            }
        });

        this.setLayout(null);//布局

        this.add(jan);
        this.add(bi);
        this.add(sysName);
        this.add(an1);
        this.add(an2);
        this.add(tu);
//        this.add(gifTxt);
        this.add(lie1);
        this.add(lie2);
        this.add(user);
        this.add(pass);
        this.add(su1);
        this.add(mi1);
        this.add(text5);
        this.add(submit);
        this.add(bgcolor);

        this.setSize(835, 500);
//        this.setIconImage(Toolkit.getDefaultToolkit().createImage("素材\\透明照片.png"));//窗体图标
        this.setLocationRelativeTo(null);//保持居中
        this.setUndecorated(true);//去顶部 让JFrame自带窗口标题栏不可见
        this.setFocusable(true);//面板首先获得焦点
        this.setBackground(new Color(255,255,255));//背景颜色
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);//最顶层
        this.setVisible(true);//显示
    }


    public static void main(String[] args) {// 启动入口
        new LoginView();
    }


//鼠标的时间监听,有很多字母用于判断状态

    public void mouseClicked(MouseEvent e) {}//点击不恢复

    public void mousePressed(MouseEvent e) {//点击后
        if (e.getSource() == jan) {
            setExtendedState(JFrame.ICONIFIED);
        }else if(e.getSource()== this) {
            origin.x = e.getX();
            origin.y = e.getY();
        }else if(e.getSource()==bi) {
            System.exit(0);
        }else if(e.getSource()==submit||e.getSource()==text5) {
            text5.setFont(new Font("微软雅黑", 0, 14));
            dispose();

            String users = user.getText();
            String password = pass.getText();
            System.out.println("您已点击登录按钮");

            if(users.equals("向到极致")&&password.equals("666666")) {//登录成功的提示弹窗,可以在此进入主界面
                JOptionPane.showMessageDialog(null, "登录成功");
            }else {//
                JOptionPane.showMessageDialog(null, "用户名:盖伦 密码:666666");
                new qwe();
            }

        }
    }

    public void mouseReleased(MouseEvent e) {//点击时
        if(e.getSource()==submit||e.getSource()==text5) {
            text5.setFont(new Font("微软雅黑", 0, 15));
        }
    }

    public void mouseEntered(MouseEvent e) {//悬停
        if (e.getSource() == jan) {
            an1.setOpaque(true);
        }else if(e.getSource()==bi) {
            an2.setOpaque(true);
        }else if(e.getSource()==user) {
            if(a==0&&c==0) {
                lie1.setIcon(new ImageIcon("src/素材/直线4.png"));
            }
        }else if(e.getSource()==pass) {
            if(b==0&&d==0) {
                lie2.setIcon(new ImageIcon("src/素材/直线4.png"));
            }
        }else if(e.getSource()==text3) {
            text3.setForeground(Color.GRAY);
        }else if(e.getSource()==text4) {
            text4.setForeground(Color.GRAY);
        }
    }

    public void mouseExited(MouseEvent e) {//悬停后
        if (e.getSource() == jan) {
            an1.setOpaque(false);
        }else if(e.getSource()==bi) {
            an2.setOpaque(false);
        }else if(e.getSource()==user) {
            if(a==0) {
                lie1.setIcon(new ImageIcon("src/素材/直线2.png"));
            }
        }else if(e.getSource()==pass) {
            if(b==0) {
                lie2.setIcon(new ImageIcon("src/素材/直线2.png"));
            }
        }else if(e.getSource()==text3) {
            text3.setForeground(new Color(170, 170, 170));
        }else if(e.getSource()==text4) {
            text4.setForeground(new Color(170, 170, 170));
        }

    }

    public void setBak() {
        ((JPanel) this.getContentPane()).setOpaque(false);
        ImageIcon img = new ImageIcon("src/素材/bgp.jpg");//这里也可以找合适的动图来作背景图显示
        JLabel background = new JLabel(img);
        this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
        background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    }
}

布局与封装、图形绘制、资源等代码在我的云盘和gitee上。

如果你的项目窗体界面不需要JFame自带的样式,需要自定义一个窗体,请参考我的另外一篇文章:带图标,标题,最小化,最大化,还原,关闭,背景色,宽,高,初始定位,窗体上面可拖拽移动窗体位置,可拖拽边框放大缩小窗体大小等。

posted @ 2022-07-11 15:00  我不是习小贵  阅读(74)  评论(0编辑  收藏  举报