You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
localhost9000/MyFrame.java

50 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.zzy.ui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends JFrame {
public MyFrame(String title, int width, int height) {
super(title); // 使用提供的标题调用父类构造函数
// 设置窗口大小
setSize(width, height);
// 获得屏幕的宽度和高度
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int ScreenWidth = screenSize.width;
int ScreenHeight = screenSize.height;
// 计算窗口居中的坐标
int x = (ScreenWidth - width) / 2;
int y = (ScreenHeight - height) / 2;
// 设置窗口的位置
setLocation(x, y);
JFrame frame = new JFrame("Login Window");
frame.setSize(new Dimension(400, 300)); // 设置窗口大小为 400x300 像素
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// 注册窗口事件
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// 退出系统
System.exit(0);
}
});
// 设置窗口可见
setVisible(true);
}
// 不需要重写setLocation和setSize方法因为JFrame已经提供了这些方法
// 可以在这里添加其他方法或组件初始化代码
}