QHYun_practice-8

先把登录界面要用的控件加进去,主要用到这些

    boolean find = false;
 
    JTextField tfAdmin ;
    JTextField tfCode ;
    JPasswordField  tfPassword;
    JTextField tfTel;
 
    JLabel msgAdmin ;
    JLabel msgCode;
    JLabel msgTel;
    JLabel msgPassword ;
 
    JProgressBar progressBar;

主要就是加Swing组件,还没有增加任何响应功能

package QHgui;
 
import QHBasic.SConst;
import QHHelper.SAbsolutePathHelper;
 
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.plaf.ProgressBarUI;
import javax.swing.plaf.basic.BasicProgressBarUI;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
 
/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/22 17:35
 */
public class QHLoginWindow extends JFrame {
    /*--------------------------fileds ---------------------------------*/
    Color color = new Color(27,67,103);
    Color color2 = new Color(27,67,103);
    private  static  final  int  Width = 1536;
    private  static  final  int Height = 864;
 
    //创建一个容器
    Container ct;
    //创建背景面板。
    BackgroundPanel bgp;
 
    //控件
    boolean find = false;
 
    JTextField tfAdmin ;
    JTextField tfCode ;
    JPasswordField  tfPassword;
    JTextField tfTel;
 
    JLabel msgAdmin ;
    JLabel msgCode;
    JLabel msgTel;
    JLabel msgPassword ;
 
    JProgressBar progressBar;
 
    /*--------------------------functions ---------------------------------*/
    public QHLoginWindow(String text) throws FileNotFoundException, IOException {
        super(text, null);
        //获取屏幕尺寸
        setSize(787, 589);
        getContentPane().setLayout(null);
 
        this.setType(JFrame.Type.UTILITY);
        this.setUndecorated(true);
 
        //移到平面正中
        moveToCenter();
        //添加背景面板
        addBackground();
 
        //添加组件
        JLabel label = new JLabel("it's all about long term!");
        label.setLocation(100, 100);
        label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        label.setSize(263, 28);
        label.setForeground(color);
        bgp.add(label);
 
        JLabel labelLog = new JLabel("Log on~");
        labelLog.setLocation(200, 200-15);
        labelLog.setFont(new Font("微软雅黑", Font.BOLD, 20));
        labelLog.setSize(150, 28);
        labelLog.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(labelLog);
 
        //用户名输入
        JLabel labelAdmin = new JLabel("Admin:");
        labelAdmin.setLocation(218, 255-15);
        labelAdmin.setFont(new Font("微软雅黑", Font.BOLD, 20));
        labelAdmin.setSize(150, 28);
        labelAdmin.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(labelAdmin);
 
        TextBorderUtlis  border = new TextBorderUtlis (new Color(192,192,192),1,true);
 
        tfAdmin = new JTextField();
        tfAdmin.setLocation(368, 255-15);
        tfAdmin.setFont(new Font("宋体", Font.BOLD, 20));
        tfAdmin.setSize(200, 28);
        tfAdmin.setBorder(border);
        bgp.add(tfAdmin);
 
        msgAdmin = new JLabel("");
        msgAdmin.setLocation(568+ 5, 255-15);
        msgAdmin.setFont(new Font("宋体", Font.BOLD, 20));
        msgAdmin.setSize(100, 28);
        msgAdmin.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(msgAdmin);
 
        //密码输入
        JLabel labelPassword = new JLabel("PassWord:");
        labelPassword.setLocation(218, 300-15);
        labelPassword.setFont(new Font("微软雅黑", Font.BOLD, 20));
        labelPassword.setSize(150, 28);
        labelPassword.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(labelPassword);
 
        tfPassword = new JPasswordField();
        tfPassword.setLocation(368, 300-15);
        tfPassword.setFont(new Font("宋体", Font.BOLD, 20));
        tfPassword.setSize(200, 28);
        tfPassword.setBorder(border);
 
        tfPassword.setEchoChar('*');
        //暂时未启用可见与不可见功能
        JButton viewBtn = new JButton(new ImageIcon(Objects.requireNonNull(SAbsolutePathHelper.getImagePath()+ File.separator+"btbk2.png")));
        //添加隐藏密码图标按钮
        JButton viewHideBtn = new JButton(new ImageIcon(Objects.requireNonNull(SAbsolutePathHelper.getImagePath()+File.separator+"btbk2.png")));
        tfPassword.putClientProperty("JTextField.trailingComponent", viewBtn);
        viewBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tfPassword.putClientProperty("JTextField.trailingComponent", viewHideBtn);//设置隐藏按钮显示,未使用FlatLightLaf则不需要
                tfPassword.setEchoChar((char) 0);//设置密码显示
            }
        });
 
        //给隐藏密码图标绑定单击事件
        viewHideBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tfPassword.putClientProperty("JTextField.trailingComponent", viewBtn);//设置显示按钮显示,未使用FlatLightLaf则不需要
                tfPassword.setEchoChar('*');//设置密码隐藏
            }
        });
 
        bgp.add(tfPassword);
 
        msgPassword = new JLabel("");
        msgPassword.setLocation(568+ 5, 300-15);
        msgPassword.setFont(new Font("宋体", Font.BOLD, 20));
        msgPassword.setSize(100, 28);
        msgPassword.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(msgPassword);
 
        //密码输入
        JLabel labelTel = new JLabel("Tel:");
        labelTel.setLocation(218, 345-15);
        labelTel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        labelTel.setSize(150, 28);
        labelTel.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(labelTel);
 
        tfTel = new JTextField();
        tfTel.setLocation(368, 345-15);
        tfTel.setFont(new Font("宋体", Font.BOLD, 20));
        tfTel.setSize(200, 28);
        tfTel.setBorder(border);
        bgp.add(tfTel);
        msgTel = new JLabel("");
        msgTel.setLocation(568+ 5, 345-15);
        msgTel.setFont(new Font("宋体", Font.BOLD, 20));
        msgTel.setSize(100, 28);
        msgTel.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(msgTel);
        //验证码
        JButton btCode = new JButton("Code:");
        btCode.setLocation(218, 390-15);
        btCode.setFont(new Font("微软雅黑", Font.BOLD, 20));
        btCode.setSize(100, 28);
 
        btCode.setHorizontalTextPosition(SwingConstants.CENTER);
 
        //设置ICon
        String imgPath1 =SAbsolutePathHelper.getImagePath()+File.separator+
                "ButtonIcon" +File.separator+"btbk2.png";
        String imgPath2 =SAbsolutePathHelper.getImagePath()+File.separator+
                "ButtonIcon" +File.separator+"btbk1.png";
        ImageIcon icon = new ImageIcon(imgPath1);
        ImageIcon icon2 = new ImageIcon(imgPath2);
        btCode.setIcon(icon);
        btCode.setRolloverIcon(icon2);
        btCode.setPressedIcon(icon2);
        //设置bt的样式
        btCode.setOpaque(false);
        btCode.setMargin(new Insets(0,0,0,0));//设置边距
        btCode.setBorderPainted(false);//不绘制边框
        btCode.setFocusPainted(false);//选中后不绘制边框
        btCode.setContentAreaFilled(false);//不绘制按钮区域
 
        bgp.add(btCode);
        btCode.addMouseListener(new MouseAdapter() {
 
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                btCode.setForeground(new Color(255,255,255));
            }
 
            public void mouseExited(MouseEvent e) {
                btCode.setForeground(new Color(0,0,0));
            }
 
        });
 
        tfCode = new JTextField();
        tfCode.setLocation(368, 390-15);
        tfCode.setFont(new Font("宋体", Font.BOLD, 20));
        tfCode.setSize(200, 28);
        tfCode.setBorder(border);
        bgp.add(tfCode);
        msgCode = new JLabel("");
        msgCode.setLocation(568+ 5, 390-15);
        msgCode.setFont(new Font("宋体", Font.BOLD, 20));
        msgCode.setSize(100, 28);
        msgCode.setHorizontalTextPosition(JLabel.CENTER);
        bgp.add(msgCode);
        //登录
        JButton btOK = new JButton("Loging");
        btOK.setLocation(218, 435-15);
        btOK.setFont(new Font("微软雅黑", Font.BOLD, 20));
        btOK.setSize(100, 28);
        btOK.setHorizontalTextPosition(SwingConstants.CENTER);
        //设置ICon
        btOK.setIcon(icon);
        btOK.setRolloverIcon(icon2);
        btOK.setPressedIcon(icon2);
        //设置bt的样式
        btOK.setOpaque(false);
        btOK.setMargin(new Insets(0,0,0,0));//设置边距
        btOK.setBorderPainted(false);//不绘制边框
        btOK.setFocusPainted(false);//选中后不绘制边框
        btOK.setContentAreaFilled(false);//不绘制按钮区域
        bgp.add(btOK);
 
        //取消
        JButton btCancel = new JButton("Cancel");
        btCancel.setLocation(368, 435-15);
        btCancel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        btCancel.setSize(100, 28);
        btCancel.setHorizontalTextPosition(SwingConstants.CENTER);
        //设置ICon
        btCancel.setIcon(icon);
        btCancel.setRolloverIcon(icon2);
        btCancel.setPressedIcon(icon2);
        //设置bt的样式
        btCancel.setOpaque(false);
        btCancel.setMargin(new Insets(0,0,0,0));//设置边距
        btCancel.setBorderPainted(false);//不绘制边框
        btCancel.setFocusPainted(false);//选中后不绘制边框
        btCancel.setContentAreaFilled(false);//不绘制按钮区域
 
        bgp.add(btCancel);
        btCancel.addMouseListener(new MouseAdapter() {
 
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                btCancel.setForeground(new Color(255,255,255));
            }
 
            public void mouseExited(MouseEvent e) {
                btCancel.setForeground(new Color(0,0,0));
            }
 
        });
        btCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 当按钮被点击时,显示一条消息
                System.exit(0);
            }
        });
 
        progressBar = new JProgressBar(JProgressBar.HORIZONTAL,0,100){
 
            @Override
            public void setUI(ProgressBarUI ui) {
                // TODO Auto-generated method stub
                super.setUI(new ProgressUI(this,new Color(255, 201, 14)));//这个颜色就是我们要设置的进度条颜色
            }
 
        };
        progressBar.setSize(787, 30);;
        progressBar.setLocation(0, 559);
        progressBar.setVisible(false);
        progressBar.setOpaque(false);
        bgp.add(progressBar);
        progressBar.setStringPainted(true);
        progressBar.setBorderPainted(false);;//设置有无边框
 
 
        //界面初始化完成后,重新界定界面和组件的大小
       // nodifyFrameSize();
    }
 
    private void nodifyFrameSize()
    {
        int fraWidth = this.getWidth();//frame的宽
        int fraHeight = this.getHeight();//fra
        // me的高
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;
 
        //重新设定界面尺寸大小
        int winWidth = fraWidth * screenWidth / Width;
        int winHeight = fraHeight *screenHeight / Height;
        this.setSize(winWidth, winHeight);
 
        float proportionW = screenWidth/fraWidth;
        float proportionH = screenHeight/fraHeight;
 
        this.modifyComponentSize(this, proportionW,proportionH);
    }
 
    public static void modifyComponentSize(JFrame frame,float proportionW,float proportionH){
 
        try
        {
            Component[] components = frame.getRootPane().getContentPane().getComponents();
            for(Component co:components)
            {
//                String a = co.getClass().getName();//获取类型名称
//                if(a.equals("javax.swing.JLabel"))
//                {
//                }
                float locX = co.getX() * proportionW;
                float locY = co.getY() * proportionH;
                float width = co.getWidth() * proportionW;
                float height = co.getHeight() * proportionH;
                co.setLocation((int)locX, (int)locY);
                co.setSize((int)width, (int)height);
                int size = (int)(co.getFont().getSize() * proportionH);
                Font font = new Font(co.getFont().getFontName(), co.getFont().getStyle(), size);
                co.setFont(font);
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
    }
 
    /**
     * 移动到中间
     */
    private void moveToCenter() {
        Container myParent = getParent();
        if (myParent == null) {
            Dimension parentSize = Toolkit.getDefaultToolkit().getScreenSize();
            Point topLeft = new Point(0, 0);
            Dimension mySize = getSize();
            int x, y;
            if (parentSize.width > mySize.width)
                x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
            else
                x = topLeft.x;
            if (parentSize.height > mySize.height)
                y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
            else
                y = topLeft.y;
            setLocation(x, y);
        } else {
            Point topLeft = myParent.getLocationOnScreen();
            Dimension parentSize = myParent.getSize();
            Dimension mySize = getSize();
            int x, y;
            if (parentSize.width > mySize.width)
                x = ((parentSize.width - mySize.width) / 2) + topLeft.x;
            else
                x = topLeft.x;
            if (parentSize.height > mySize.height)
                y = ((parentSize.height - mySize.height) / 2) + topLeft.y;
            else
                y = topLeft.y;
            setLocation(x, y);
        }
    }
 
    /**
     * 添加背景
     * @throws IOException
     * @throws FileNotFoundException
     */
    private void addBackground() throws FileNotFoundException, IOException {
        //不采用任何布局方式。
        ct = this.getContentPane();
        getContentPane().setLayout(null);
 
        //在这里随便找一张400*300的照片既可以看到测试结果。
        bgp = new BackgroundPanel();
        bgp.setBounds(0, 0, 787, 589);
        bgp.setBackground(null);
        bgp.setOpaque(false);
        ct.add(bgp);
    }
 
    public class TextBorderUtlis extends LineBorder
    {
 
        private static final long serialVersionUID = 1L;
 
        public TextBorderUtlis(Color color, int thickness, boolean roundedCorners)
        {
            super(color, thickness, roundedCorners);
        }
 
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
        {
 
            RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Color oldColor = g.getColor();
            Graphics2D g2 = (Graphics2D) g;
            int i;
            g2.setRenderingHints(rh);
            g2.setColor(lineColor);
            for (i = 0; i < thickness; i++)
            {
                if (!roundedCorners){
                    g2.drawRect(x + i, y + i, width - i - i - 1, height - i - i - 1);
                }else{
                    g2.drawRoundRect(x + i, y + i, width - i - i - 1, height - i - i - 1, 5, 5);}
            }
            g2.setColor(oldColor);
        }
 
    }
 
    public class ProgressUI extends BasicProgressBarUI {
 
        private JProgressBar jProgressBar;
        private int progressvalue;
        private Color forecolor;
 
        ProgressUI(JProgressBar jProgressBar,Color forecolor) {
            this.jProgressBar = jProgressBar;
            this.forecolor=forecolor;
        }
 
        @Override
        protected void paintDeterminate(Graphics g, JComponent c) {
            progressvalue=this.jProgressBar.getValue();
 
            if(progressvalue<20){
                this.jProgressBar.setForeground(new Color(176,224,230));
            }
            else if(progressvalue<40){
                this.jProgressBar.setForeground(new Color(135,206,235));
            }
            else if(progressvalue<60){
                this.jProgressBar.setForeground(new Color(64,224,208));
            }
            else if(progressvalue<80){
                this.jProgressBar.setForeground(new Color(51,161,201));
            }
            else{
                this.jProgressBar.setForeground(new Color(30,144,255));
            }
 
            super.paintDeterminate(g, c);
        }
    }
 
 
}

运行结果,但是我感觉中文可能还好一些:

 

posted @ 2025-07-21 09:26  Wind_Swing_Dunn  阅读(5)  评论(0)    收藏  举报