Jframe setDefaultCloseOperation 不工作

Jframe setDefaultCloseOperation not working

import javax.swing.*;
import java.awt.*;
class Myframe extends Frame
{
    private JButton btn;
    private JTextArea txtarea;
    Myframe()
    {
        super("Saibaba");
        setLayout(new BorderLayout());
        btn=new JButton("CLICK Me");
        txtarea=new JTextArea();
        add(txtarea,BorderLayout.CENTER);
        add(btn,BorderLayout.SOUTH);
        setSize(500,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //this isnt working.
        setVisible(true);
    }

    public static void main(String args[])
    {
        Myframe m=new Myframe();

    }
}

为什么这个 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 不起作用? 这个说法有什么问题?谁能纠正我?

我试过使用 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 等参数变体调用相同的方法 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 但其中 none 正在工作。

您的 class 应该扩展 JFrame class:

import javax.swing.JFrame;

class Myframe extends JFrame