2.1登录界面-先吧界面搭起来

        初步的想法就是这样的:

        看起来也比较简单。

        首先把界面搭起来再说吧,现在用的是笔记本,但是回家写可能用台式机,两个还是区别很大的,不希望维护和更新的时候,为界面尺寸和适应的问题挠头,所以先想好界面和屏幕的自适应问题。

        在QHEntrance下建一个QHgui的包,用来存放主界面相关的类。然后建一个QHLoginWindow类作为登录界面。继承JFrame.

package QHgui;

import QHBasic.SConst;

import javax.swing.*;
import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <p> TODU </P>
 *
 * @author yeager
 * @Version V1.1.1.0
 * @date 2023/11/22 17:35
 */
public class QHLoginWindow extends JFrame {
    // filed
    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;
    //functions
    public QHLoginWindow(String text) throws FileNotFoundException, IOException {
        super(text, null);
        //获取屏幕尺寸
        setSize(400,300);
        getContentPane().setLayout(null);

        this.setType(JFrame.Type.UTILITY);
        this.setUndecorated(true);

        moveToCenter();
        //界面初始化完成后,重新界定界面和组件的大小
        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);
        }
    }
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值