【Java】JFrame基本參數(shù)設(shè)置

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();
        // 設(shè)置窗體自動調(diào)節(jié)大小
        frame.pack();
        // 設(shè)置窗體位置、大小
        frame.setBounds(100, 100, 100, 100);
        // 設(shè)置窗體是否可調(diào)節(jié)大小
        frame.setResizable(false);
        // 設(shè)置窗體布局
        frame.getContentPane().setLayout(null);
        // 設(shè)置窗體標(biāo)題
        frame.setTitle("Frame");
        // 設(shè)置窗體字體
        frame.setFont(new Font("黑體", Font.PLAIN, 17));
        // 設(shè)置窗體在屏幕中央打開
        frame.setLocationRelativeTo(null);
        // 設(shè)置窗體默認(rèn)關(guān)閉方式為退出程序
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 設(shè)置窗體圖標(biāo)
frame.setIconImage(ImageIO.read(this.getClass().getResource("/priv/image/image.png")));
        // 設(shè)置窗體觀感(皮膚/主題)
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        // 設(shè)置窗體是否可見
        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();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容