Java设计上位机—渐变色登录界面

2022-07-27,,,,

Java设计上位机——渐变色登录界面

先上效果图

进入正题

利用继承Jpanel类来进行实现,将Jpanel填充满整个窗口(Jframe)就可以实现了。
然后利用GradientPaint类来实现渲染渐变。

public class ColorJpanel extends JPanel {
    public ColorJpanel() {
        super();
    }
    @Override
    protected void paintComponent(Graphics g1) {// 重写绘制组件外观
        Graphics2D g = (Graphics2D) g1;
        super.paintComponent(g);// 执行超类方法
        int width = getWidth();// 获取组件大小
        int height = getHeight();
        
        // 创建填充模式对象,参数分别是起点坐标下x,y,起点颜色,终点坐标下x,y,终点颜色。
        GradientPaint paint = new GradientPaint(0, 0, new Color(220,123,127), width, height, new Color(127,200,200));
        
        g.setPaint(paint);// 设置绘图对象的填充模式
        g.fillRect(0, 0, width, height);// 绘制矩形填充控件界面
    }
}

这样就可以像使用Jpanel一样创建自己的类ColorJpanel了,使用方式基本一样。
例如:

        //登录界面
        JFrame jlogin =new JFrame("登录界面");
        jlogin.setSize(600,350);
        jlogin.setLocation(screenWidth/2-jlogin.getWidth()/2,screenHeight/2-jlogin.getHeight()/2);
        jlogin.setResizable(false);

        /**
         * 配置登录界面
         */
        ColorJpanel loginJpanel =new ColorJpanel();
        loginJpanel.setBounds(0,0,jlogin.getWidth(),jlogin.getHeight());
        loginKey = new JButton("登录");
        loginKey.setMargin(new Insets(1,1,1,1));
        loginKey.setBounds(new Rectangle(250,250,100,30));

        JLabel welcome = new JLabel("欢迎使用一体化管理系统");
        welcome.setFont(new Font("宋体",Font.PLAIN,40));
        welcome.setBounds(new Rectangle(50,30,500,80));
        jlogin.add(welcome);

        setAcount();
        JTextField id = new JTextField("请输入账户名");
        id.setBounds(new Rectangle(200,150,200,25));
        id.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                id.setText("");
            }
            @Override
            public void focusLost(FocusEvent e) {
                id.setText(id.getText());
            }
        });
        JPasswordField password = new JPasswordField("请输入密码");
        password.setBounds(new Rectangle(200,180,200,25));
        password.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                password.setText("");
            }
            @Override
            public void focusLost(FocusEvent e) {
            }
        });
        JLabel idLabel = new JLabel("账户:");
        idLabel.setBounds(new Rectangle(150,150,50,25));
        JLabel psLabel = new JLabel("密码:");
        psLabel.setBounds(new Rectangle(150,180,50,25));

        jlogin.add(id);
        jlogin.add(password);
        jlogin.add(idLabel);
        jlogin.add(psLabel);

        jlogin.add(loginKey);
        jlogin.setLayout(null);
        jlogin.add(loginJpanel);
        jlogin.setVisible(true);
        loginKey.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String idinput = id.getText();
                String pasinput = new String(password.getPassword());

                if(acount.keySet().contains(idinput)){
                    if(pasinput.equals(acount.get(idinput))) {
                        jlogin.setVisible(false);
                        j.setVisible(true);
                    }else {
                        JOptionPane.showMessageDialog(null,"密码错误,请检查后重新输入!");
                    }
                }else {
                    JOptionPane.showMessageDialog(null,"账号不存在,请检查后重新输入!");
                }
            }
        });
        jlogin.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        

添加控件不显示的问题(例如JButton)

不过需要注意一点,如果这里的布局方式设置为null,那么你在这个ColorJanel中插入的空间等,就需要规划具体的位置了,不能直接添加,否则可能添加了控件但是不显示的情况

        JLabel idLabel = new JLabel("账户:");
        //规划Jlabel位置
        idLabel.setBounds(new Rectangle(150,150,50,25));

本文地址:https://blog.csdn.net/weixin_44005494/article/details/110240864

《Java设计上位机—渐变色登录界面.doc》

下载本文的Word格式文档,以方便收藏与打印。