初步的想法就是这样的:
看起来也比较简单。
首先把界面搭起来再说吧,现在用的是笔记本,但是回家写可能用台式机,两个还是区别很大的,不希望维护和更新的时候,为界面尺寸和适应的问题挠头,所以先想好界面和屏幕的自适应问题。
在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);
}
}
}
运行结果: