mamur
mamur

Reputation: 11

Can't close window using setDefaultCloseOperation

I am new in Java and I can't find my error in this simple code. The error is close operation doesn't work:

error: cannot find symbol

Above is the compilation error. Here is the code.

import javax.swing.*;
import java.awt.*;
class UseButton extends Frame {

    public static void main(String...args) {
        UseButton btn = new UseButton();
        btn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btn.setSize(200, 150);
        btn.setVisible(true);

    }
    private JButton b;
    private JTextField text;

    public UseButton() {
        super("title");
        setLayout(new FlowLayout());

        b = new JButton("OK");
        add(b);
    }
}

Upvotes: 0

Views: 3537

Answers (2)

Lpia IT
Lpia IT

Reputation: 1

The error is close operation doesn't work:

error: cannot find symbol

cause using setDefaultCloseOperation only in JFrame (Swing library). Yes I'm agree with @MadProgrammer.

so my analyze, you want to using Frame in AWT library. Like this following code:

    import java.awt.event.*;
    import java.awt.*;
    class UseButton extends Frame {

    public static void main(String...args) {
        UseButton btn = new UseButton();

        btn.setSize(200, 150);
        btn.setVisible(true);

    }
    private JButton b;
    private JTextField text;

    public UseButton() {
        super("title");
        setLayout(new FlowLayout());

        b = new JButton("OK");
        add(b);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });

    }
}

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347214

java.awt.Frame does not have a method called setDefaultCloseOperation, I think you want to use javax.swing.JFrame

Having said that, you shouldn't be extending directory from a top level container like JFrame, this is bad practice as you're not really adding any value to the class, reduces the re-usability of your class (as you can't add it to anything else) and locks you into a single presentation implementation...you can add it to anything else...

Bad...

class UseButton extends Frame{

Good...

class UseButton extends JFrame{

Better...

import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class UseButton {

    public static void main(String... args) {
        new UseButton();

    }

    public UseButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton b;
        private JTextField text;

        public TestPane() {
            b = new JButton("OK");
            add(b);
        }

    }
}

Upvotes: 8

Related Questions