📄 mainframe.java
字号:
import java.awt.*;
import javax.swing.*;
public class MainFrame {
// 我的显示器设置为1024*768,因此下列常量的值分别是:341(width), 192(height), 341(startX), 256(startY)
private static int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 3;
private static int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 4;
private static int startX = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 3;
private static int startY = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 3;
private static JFrame frame;
private static JPanel contentPane;
// 防止使用者创建MainFrame对象
private MainFrame() { }
public static void init() {
init("欢迎进入Java语言的GUI世界");
}
public static void init(String title) {
frame = new JFrame(title);
frame.setLocation(new Point(startX, startY));
contentPane = (JPanel)frame.getContentPane();
contentPane.setPreferredSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void init(String title, int w, int h) {
width = w; height = h;
init(title);
}
public static void init(String title, int w, int h, int x, int y) {
startX = x; startY = y;
width = w; height = h;
init(title);
}
public static void init(String title, int w, int h, int x, int y, String lookAndFeel) {
startX = x; startY = y;
width = w; height = h;
try {
if (lookAndFeel.equalsIgnoreCase("windows"))
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
else if (lookAndFeel.equalsIgnoreCase("system"))
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
else if (lookAndFeel.equalsIgnoreCase("motif"))
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
else UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {}
init(title);
}
public static void add(Component c) { contentPane.add(c, BorderLayout.CENTER); }
public static void start() {
frame.pack();
frame.setVisible(true);
}
public static JPanel getContentPane() { return contentPane; }
public static JFrame getMainFrame() { return frame; }
public static int getWidth() { return width; }
public static int getHeight() { return height; }
public static int getStartX() { return startX; }
public static int getStartY() { return startY; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -