主界面的写法跟之前登录界面的窗体写法大致相同,在主界面中主要是窗体的大小的设置
package com.student_view;
import com.utils.DimensionUtil;
import sun.applet.Main;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
/**
* @Author:xiexu
* @Date:2023/12/9 10:36
*/
public class MainView extends JFrame {
public MainView() {
super("学生成绩管理系统");
Container contentPane = getContentPane();
URL url = MainView.class.getClassLoader().getResource("tumblr.png");
setIconImage(new ImageIcon(url).getImage());
// setSize(800, 600);
// 需要根据屏幕的大小,设置主界面窗口的大小:
setBounds(DimensionUtil.getBounds());
// 设置窗体完全充满整个屏幕的可见大小
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
setLocationRelativeTo(null);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MainView();
}
}
封装一个工具类:来获取电脑屏幕的大小
package com.utils;
import javax.swing.*;
import java.awt.*;
/**
* @Author:xiexu
* @Date:2023/12/9 10:47
*/
public class DimensionUtil {
public static Rectangle getBounds() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取电脑屏幕的大小
// 需要保证主界面不会覆盖电脑屏幕的任务栏
Insets screenInsets = Toolkit.getDefaultToolkit()
.getScreenInsets(new JFrame().getGraphicsConfiguration());
// 需要使得左右两边完全填充,此时也需要设置setResizable(true);
Rectangle rectangle = new Rectangle(screenInsets.left, screenInsets.top,
screenSize.width - screenInsets.left - screenInsets.right,
screenSize.height - screenInsets.top - screenInsets.bottom);
return rectangle;
}
}
运行结果展示: