1,利用内部类构造方法创建窗口

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

//先调用要用到的包



public class JFrame_test extends JFrame implements ActionListener{

//继承JFrame类实现ActionListener接口,JFrame类创建窗口,ActionListener接口实现事件监听
    public JFrame_test(){
        super("窗口");//父类构造方法设置窗口名称

        addWindowListener(new window());
    }

    public static void main(String[] args){

        JFrame_test jf=new JFrame_test();//实例化对象

        jf.setVisible(true);//设置窗口是否显示为true

        jf.setSize(500,500);//设置窗口大小,参数为width与height

        jf.setLocation(500,50);//设置窗口显示位置,参数为x,y(屏幕上的横向x与纵向y)
        // jf.setBounds(int x,int y,int width,int height);方法可以同时设置窗口显示位置和窗口大小

    }

    public void actionPerformed(ActionEvent actionEvent){
        //实现未完成的方法(在这里写要执行的事件)

    }

    class window extends WindowAdapter{

        public void windowClosing(WindowEvent e){
            Window window=e.getWindow();
            window.dispose();//关闭窗口
        }
    }
}