Question

I'm trying to dispose of the difficulty window after any one of the difficulty button's are clicked but it won't happen. I've tried .dispose and frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); but i can't get it. Is it just placement or more?

import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;

public class Game extends JFrame{

    public static JFrame frame = new JFrame();


    private JLabel lab;

public static void main(String[] args) {

    Game difficulty = new Game();
    difficulty.setSize(350,105);
    difficulty.setTitle("Difficulty.");
    difficulty.setVisible(true);
    difficulty.setLocationRelativeTo(null);


    /**Game sudoku = new Game();
    sudoku.setSize(900, 900);
    sudoku.setVisible(false);*/

}   


public Game(){

    setLayout(new FlowLayout());
    lab = new JLabel("Please select your difficulty.");
    add(lab);

    JButton easy;
    easy = new JButton("Easy");
    add(easy);

     easy.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked the button");
                JFrame.dispose();
            }
        });   


    JButton medium;
    medium = new JButton("Medium");
    add(medium);

    JButton hard;
    hard = new JButton("Hard");
    add(hard);

    JButton evil;
    evil = new JButton("Evil!");
    add(evil);

}
}
Was it helpful?

Solution 2

dispose() method is not a static, so it'll not work by calling it directly from JFrame class

JFrame.dispose();

try to do :

dispose();

Or to dispose the frame object you have created

frame.dispose();

Read more about JFrame

OTHER TIPS

First of all you're extending JFrame and creating an object of JFrame, if I'm not wrong, this shouldn't be done.

public class Game extends JFrame{

    public static JFrame frame = new JFrame();

And as @Salah said, JFrame is not static, so it should be:

public JFrame frame = new JFrame();

To solve your problem, you're disposing a new JFrame (yes, you have 3 JFrames in one class, instead of 1, which is what you want), with: JFrame.dispose(); if you already created an object or you're extending JFrame, you can:

this.dispose(); //For the extended JFrame

or

frame.dispose(); //For the object you created

I had the same problem:

this.dispose();

solved my problem.

Try setting the jFrame to invisible before disposing it:

public void disposeJFrame(JFrame frame){
    frame.setVisible(false);
    frame.dispose();
}

If you're wanting to close the whole program, you can use System.exit(0);

Instead JFrame.dispose();, use frame.dispose() or JFrame.this.dispose();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top