-3

I am relatively new at java and I am making a HangMan Game but I am facing a problem right now.In particular, I want to close the frame of the program to open a new one when someone finds the word but since my class there extends JPanel dispose() isn't an available solution. So, what should I do to close this frame? ( bare in mind that its object is defined at another class, the one that calls that specific one.) Here is part of my code: ( please forgive me for displaying so many lines ) I just want to know how I can close this window without exiting the whole program, Thanks for your time :) )

EDIT: Thanks guys, I really learned a lot through this '' comment section''. I am grateful for your quick responds and I hope that in the future i will not ask such a silly question again:) PS: I actually used the CardLayout to solve my problem. I don't really see thought how this question is similar to '' How to programmatically close a JFrame '' since in this one I can't use the dispose method because i am extending a JPanel.

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class Game extends JPanel implements Runnable{

public void paintComponent(Graphics g)
{
 super.paintComponent(g);
 this.setBackGround(Color.BLACK);

}


public void run()
{
}
 }
  • 3
    Please post the [minimum amount of code](http://stackoverflow.com/help/mcve) that will demonstrate your problem. This is far too much irrelevant code to expect anyone to read. – khelwood Feb 09 '16 at 20:39
  • 1
    *"I want to close the frame of the program to open a new one*" - Plese consider alternatives like a `CardLayout`, the [use of multiple `JFrame`s](http://stackoverflow.com/q/9554636/4857909) isn't good practise. – Lukas Rotter Feb 09 '16 at 20:43
  • *" I want to close the frame of the program to open a new one when someone finds the word"* - Don't. Reset the state of the game to the beginning again – MadProgrammer Feb 09 '16 at 23:43

4 Answers4

2

I want to close the frame of the program to open a new one when someone finds the word

The simple answer is, don't. Instead, reset the state of the game to a initial state and update the UI accordiningly

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

For your problem:

Also, using a text field with a keyboard listener or a "submit" button may make this easier - you won't need the 26 letter buttons and the UI would be much more intuitive.

Also, using arrays would make many of your other variables disappear.

Heman Gandhi
  • 1,343
  • 9
  • 12
  • but, can i use the paintComponent method without extending jpanel? – Notorious_Hacker Feb 09 '16 at 20:46
  • No. What exactly do you need paintComponent for? You only seem to be updating the UI when the user clicks. – Heman Gandhi Feb 09 '16 at 20:49
  • i am actually drawing the hangman in the program – Notorious_Hacker Feb 09 '16 at 21:24
  • *"but, can i use the paintComponent method without extending jpanel?"* - No – MadProgrammer Feb 09 '16 at 23:42
  • 1
    As a general rule of thumb, it's a bad idea to extend from top level containers like `JFrame`, 99% of the time, you're not adding new functionality to the class and you're just locking yourself into a single use case. 99% of the time, you can achieve the same results as extending from `JFrame` as you can by simply creating a new instance of it when you need it – MadProgrammer Feb 09 '16 at 23:45
0

Maybe wrap the entire Game class in a parent class that can control the creation of new JFrames, and also the addition of a Game JPanel to it?

That would logically separate the components, because it can be confusing when a component class - to be added to a container - is extended and then proceeds to create its own container (this is actually fine if the UI setup is done once, but possibly not for a repeatable process).

HomerPlata
  • 1,687
  • 5
  • 22
  • 39
0

The JPanel alone cannot be visible, you need to have it in some sort of container. This container is received by panel.getParent().

Having that, you can remove yourself from the parent container.

This sample class has a JButton that removes the panel from the view hierarchy when clicked.

public static class APanel extends JPanel {
    public APanel() {
        super();

        JButton btn = new JButton();
        btn.setText("Hello World!");
        btn.addActionListener(e -> {
            ((JButton) e.getSource()).setText("Boo!");
            final Container parent = APanel.this.getParent();
            parent.remove(APanel.this);
            parent.repaint();
        });
        add(btn);
    }
}
thst
  • 4,592
  • 1
  • 26
  • 40
  • this actually helped me with another program of mine but is there something i can do to close this specific window or do i have to use CardLayout? thx for your time – Notorious_Hacker Feb 09 '16 at 21:28
  • @Notorious_Hacker What do you mean with "this specific window"? You have a `JPanel` presented in your code. The panel is not a window (`JFrame` is a window). A `JPanel`, beeing a `JComponent` can be `add()`ed and `remove()`d from a `Container` class. It cannot be closed. Btw: If you found my comment useful, it is good practice on SO to vote up. – thst Feb 09 '16 at 21:43