-1

I have a problem which is most likely "simple" however I can't figure it out. I am trying to reference my current JFrame so that I can dispose of it, and create a new one, thus "resetting" the program, however I and having trouble figuring out how to reference the JFrame, I have tried, super, this and getParent(), but none of the seem to work. Thanks for any / all help. ^^

Here is my code:

Main Class, just sets up the Jframe and calls the class that creates everything:

    public static void main(String args[]) {
    JFrame window = new JFrame();
    Director director = new Director(window, args);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLocationRelativeTo(null);
    window.pack();
    window.setVisible(true);
}

}

Class the creates everything:

    import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class Director extends JFrame implements CollisionListener {

    private BrickWall wall;
    private JLabel gameTitle, gameScore, gameLives;
    private JPanel controlPanel;
    private JButton reset, quit;
    private JRadioButton hard, normal, easy;
    private int score = 6, lives = 5;
    private ButtonGroup difficulty;


    public Director(JFrame window, String[] args) {
        window.getContentPane().add(makeGamePanel(), BorderLayout.CENTER);
        window.getContentPane().add(gameControlPanel(), BorderLayout.NORTH);

    }


    public void collisionDetected(CollisionEvent e) {
        wall.setBrick(e.getRow(), e.getColumn(), null);
    }

    private JComponent makeGamePanel() {
        wall = new BrickWall();
        wall.addCollisionListener(this);
        wall.buildWall(3, 6, 1, wall.getColumns(), Color.GRAY);
        return wall;
    }

    // Reset method I'm trying to dispose of the JFrame in.
    private void reset() {
        JFrame frame = new JFrame();

        frame.getContentPane().add(makeGamePanel(), BorderLayout.CENTER);
        frame.getContentPane().add(gameControlPanel(), BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    private JComponent gameControlPanel() {

        // CONTROL PANEL PANEL!
        controlPanel = new JPanel();

        gameTitle = new JLabel("Brickles");
        gameScore = new JLabel("Score:" + "   " + score);
        gameLives = new JLabel("Lives:" + "   " + lives);
        reset = new JButton("Reset");
        quit = new JButton("Quit");
        hard = new JRadioButton("Hard", false);
        normal = new JRadioButton("Normal", true);
        easy = new JRadioButton("Easy", false);
        difficulty = new ButtonGroup();
        difficulty.add(hard);
        difficulty.add(normal);
        difficulty.add(easy);
        controlPanel.setLayout(new GridLayout(4, 2));
        controlPanel.add(gameTitle);
        controlPanel.add(gameScore);
        controlPanel.add(hard);
        controlPanel.add(gameLives);
        controlPanel.add(normal);
        controlPanel.add(reset);
        controlPanel.add(easy);
        controlPanel.add(quit);

        // Action Listener, where I'm caling the reset method.

        reset.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                reset();
            }
        });
        return controlPanel;
    }

}
user1719605
  • 189
  • 1
  • 15

2 Answers2

4

You can refer to the "outer this" from a nested class with the following syntax:

reset.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
         Director.this.reset();
    }
});
DSquare
  • 2,458
  • 17
  • 19
4

Yes, you can refer to the outer class by specifying it with the class name as noted in DSquare's good answer (1+ to it), but I urge you not to fling JFrame's at the user as you're program is trying to do. I recommend:

  • Instead of opening and closing multiple JFrames, use only one JFrame as the main application's window.
  • If you need helper windows, such as modal windows to get critical information that is absolutely needed, before the program can progress, use modal dialogs such as JDialogs or JOptionPanes.
  • If you need to swap GUI's, instead of swapping JFrames, swap "views" inside the JFrame via a CardLayout.
  • Gear your code towards creating these JPanel views and not JFrames as it will make your Swing GUI's much more flexible and portable.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I tried to do that, but when I tried "Resetting" the values, such as score, lives and the wall, it wouldn't change anything. Could this be because I need to readd the contentPane()'s, in my reset method? If so how I would approach that? Also; thanks for your help. – user1719605 Jun 01 '14 at 16:31
  • 1
    @user1719605: You're doing it wrong, perhaps resetting the wrong reference, but who knows without code. I suggest that you ask this as a separate question, showing your code attempt, and let's see if we can give you a more complete answer. – Hovercraft Full Of Eels Jun 01 '14 at 16:41