Jframe setDefaultCloseOperation not working

痞子三分冷 提交于 2019-12-12 18:22:45

问题


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();

    }
}

Why is this setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); not working? What's wrong with this statement? Can anyone correct me?

I have tried calling same Method with parameter variants like setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); and setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); but none of them is working.


回答1:


Your class should extend the JFrame class:

import javax.swing.JFrame;

class Myframe extends JFrame


来源:https://stackoverflow.com/questions/34250434/jframe-setdefaultcloseoperation-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!