【Java】JFrame基本参数设置

import java.io.IOException;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.imageio.ImageIO;

public class Frame implements WindowListener {
    public static JFrame frame;
    public Frame() {
        initialize();
    }
    
    private void initialize(){
        // 新建窗体
        frame = new JFrame();
        // 设置窗体自动调节大小
        frame.pack();
        // 设置窗体位置、大小
        frame.setBounds(100, 100, 100, 100);
        // 设置窗体是否可调节大小
        frame.setResizable(false);
        // 设置窗体布局
        frame.getContentPane().setLayout(null);
        // 设置窗体标题
        frame.setTitle("Frame");
        // 设置窗体字体
        frame.setFont(new Font("黑体", Font.PLAIN, 17));
        // 设置窗体在屏幕中央打开
        frame.setLocationRelativeTo(null);
        // 设置窗体默认关闭方式为退出程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗体图标
frame.setIconImage(ImageIO.read(this.getClass().getResource("/priv/image/image.png")));
        // 设置窗体观感(皮肤/主题)
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        // 设置窗体是否可见
        frame.setVisible(true);
        // 添加 WindowListener
        frame.addWindowListener(this);
    }
    
    /**
     * @description 重写 WindowListener
     * @param e
     */
    public void windowClosing(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }
    
    public static void main(String args[]) {
        new Frame();
    }
}

你可能感兴趣的:(【Java】JFrame基本参数设置)