I am making a battleship game with 2 boards, each is on a JFrame, but I do not know how to get them to close when a button is pressed in order to show the new one.

package battleship;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GuiBoard extends JFrame
{
    private JPanel pane = new JPanel();
    private JButton squares[][];

    public GuiBoard()
    {
    }

    public GuiBoard(int b[][], int n)
    {

        pane.setLayout(new GridLayout(b.length/2,b.length));
        squares = new JButton[b.length][b.length];

        if(n == 1)
        {
            setSize(700,450);
            setTitle("BattleShip Human Board");
            setLocation(0,0);
            buildSpotsHuman(b);
        }
            else
        {
            setSize(700,450);
            setTitle("BattleShip Computer Board");
            setLocation(0,500);
            buildSpotsComputerOff(b);

        }

    }

    public GuiBoard(int b[][], Sound s)
    {

        pane.setLayout(new GridLayout(b.length/2,b.length));
        squares = new JButton[b.length][b.length];


            setSize(700,450);
            setTitle("BattleShip Computer Board");
            setLocation(0,500);
            buildSpotsComputerOn(b, s);


    }



    public final void buildSpotsHuman(int board[][])
    {
        squares = new JButton[board.length][board.length];
        for(int i = 0;i < board.length/2;i++)
        {
            for(int j=0;j < board.length;j++)
            {
                if(board[i][j] == 1){
                    squares[i][j] = new JButton("+");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setEnabled(false);
                    squares[i][j].setBackground(Color.GRAY);
                    pane.add(squares[i][j]);
                }
                else if(board[i][j] == 3){
                    squares[i][j] = new JButton("o");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setBackground(Color.RED);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                else if (board[i][j] == 2){
                    squares[i][j] = new JButton("~");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setBackground(Color.BLUE);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                else
                {
                    squares[i][j] = new JButton("-");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setBackground(Color.CYAN);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                repaint();
                add(pane);

           }
        }
     }

    public final void buildSpotsComputerOn(int board[][], Sound s)
    {
        squares = new JButton[board.length][board.length];
        for(int i = board.length/2; i < board.length;i++)
        {
            for(int j = 0;j < board.length;j++)
            {
                if(board[i][j] == 3){
                    squares[i][j] = new JButton("o");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);

                }
                else if (board[i][j] == 2){
                    squares[i][j] = new JButton("~");
                    squares[i][j].setName(i+","+j);
                    pane.add(squares[i][j]);
                    squares[i][j].setEnabled(false);
                }
                else
                {
                    squares[i][j] = new JButton("-");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].addActionListener(new ButtonListener(board, s));
                    pane.add(squares[i][j]);


                }
                repaint();
                add(pane);


            }
        }
     }

    public final void buildSpotsComputerOff(int board[][])
    {
        squares = new JButton[board.length][board.length];
        for(int i = board.length/2; i < board.length;i++)
        {
            for(int j = 0;j < board.length;j++)
            {
                if(board[i][j] == 3){
                    squares[i][j] = new JButton("o");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                else if (board[i][j] == 2){
                    squares[i][j] = new JButton("~");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                else
                {
                    squares[i][j] = new JButton("-");
                    squares[i][j].setName(i+","+j);
                    squares[i][j].setEnabled(false);
                    pane.add(squares[i][j]);
                }
                repaint();
                add(pane);


            }
        }
    }
    public void close(){

        WindowEvent winClosingEvent = 
                new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);

    }   

}


package battleship;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonListener extends JFrame implements ActionListener
{   
    int board [][];
    Sound sound;
    LinkedList list = new LinkedList();

    public ButtonListener(int [][] b, Sound s)
    {
        board = b;
        sound = s;
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {   

        Addons a = new Addons();
        GuiBoard guiHuman = new GuiBoard();
        Computer c = new Computer();
        Human h = new Human();



        JButton button = (JButton)e.getSource();

        int x = Integer.parseInt(String.valueOf(button.getName().charAt(0)));
        int y = Integer.parseInt(String.valueOf(button.getName().charAt(2)));

        if(board[x][y] == 1)
        {
            button.setText("oo");
            button.setEnabled(false);
            sound.playExplo();
            list.addSorted(new Node(y,x));
            h.setHits(list);
            board[x][y] = 3;
        }
        else if (board[x][y] == 0)
        {
            button.setText("~~");
            button.setEnabled(false);
            sound.playMiss();
            board[x][y] = 2;

        }

        h.play(board);

        if(a.checkWinner(board))
        {
            a.sleep(3);
            c.play(board);
            guiHuman = new GuiBoard(board, 1);
            guiHuman.setDefaultCloseOperation(guiHuman.DISPOSE_ON_CLOSE);
            guiHuman.setVisible(true);

        }

    }    
}



package battleship;


import javax.swing.JOptionPane;

public class BattleShip {

    /********************************************************
     * 
     *                     MAIN
     * Purpose: Calls most methods, sets Mode, checks for
     *          winner and prints linked lists with hits
     * 
     * @param args
     * 
     *******************************************************/
    public static void main(String[] args) 
    {
        Addons a = new Addons();
        Computer c = new Computer();
        Human h = new Human();
        Board b = new Board();
        Sound s = new Sound();
        String input = "";
        GuiBoard guiHuman = new GuiBoard();
        GuiBoard guiComputer = new GuiBoard();
        s.playIntro("start");
        Instructions i = new Instructions();
        i.setVisible(true);

        while(a.intro())
        {
            do
            {
                try
                {
                    input = JOptionPane.showInputDialog(
                        "What Mode do you want to play? E, M or H?").toUpperCase();
                     switch(input)
                    {
                    case "E":
                        b = new Board("easy");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiHuman.setDefaultCloseOperation(guiHuman.DISPOSE_ON_CLOSE);
                        guiHuman.setVisible(true);
                        guiComputer = new GuiBoard(b.getBoard(), 2);
                        guiComputer.setDefaultCloseOperation(guiComputer.DISPOSE_ON_CLOSE);
                        guiComputer.setVisible(true);
                        break;
                    case "M": 
                        b = new Board("medium");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiHuman.setDefaultCloseOperation(guiHuman.DISPOSE_ON_CLOSE);
                        guiHuman.setVisible(true);
                        guiComputer = new GuiBoard(b.getBoard(), 2);
                        guiComputer.setDefaultCloseOperation(guiComputer.DISPOSE_ON_CLOSE);
                        guiComputer.setVisible(true);
                        break;
                    case "H":
                        b = new Board("hard");
                        b.prepareBoard();
                        guiHuman = new GuiBoard(b.getBoard(), 1);
                        guiHuman.setDefaultCloseOperation(guiHuman.DISPOSE_ON_CLOSE);
                        guiHuman.setVisible(true);
                        guiComputer = new GuiBoard(b.getBoard(), 2);
                        guiComputer.setDefaultCloseOperation(guiComputer.DISPOSE_ON_CLOSE);
                        guiComputer.setVisible(true);
                        break;
                    default:
                        break;

                    }
                }
                catch (NullPointerException npe)
                {
                    System.exit(0);
                }
            }while(a.checkMode(input));



            a.countDown(5, s);
            guiComputer.close();

            guiComputer = new GuiBoard(b.getBoard(), s);
            guiComputer.setDefaultCloseOperation(guiComputer.DISPOSE_ON_CLOSE);
            guiComputer.setVisible(true);

            a.sleep(5);
            guiHuman.close();

            while(a.checkWinner(b.getBoard()))
            {}

        s.playIntro("stop");

        if(!h.getHits().isEmpty()) 
        {
            System.out.println("Human:");
            h.getHits().printLinkedList();
        }




        }
    }
}

Call the dispose() method on the frame you want to close.

I am using the dispose on the guiboards which i send into buttonlistener, and only one window (Human) is disposing of older ones, anything I don't see?

package battleship;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ButtonListener extends JFrame implements ActionListener
{   
    int board [][]; // 2D array of integers to hold miss and hits
    Sound sound; // Reference to sound Class
    LinkedList list = new LinkedList(); // LinkedList for hits
    GuiBoard guiHuman = new GuiBoard(); // Reference to Human Board
    GuiBoard guiComputer = new GuiBoard();// Reference to Computer Board

    /*******************************************************
     * Constructor GuiBoard
     * 
     * Purpose: Assigns information to local variables
     * 
     * @param b [][] - 2D Array of ints which holds values
     *                      of misses and hits
     * 
     * @param s - reference to Sound class
     * 
     * @param h1 - reference to Human Board
     * 
     * @param c1 - reference to Computer Board
     * 
     *******************************************************/
    public ButtonListener(int [][] b, Sound s, GuiBoard h1, GuiBoard c1)
    {
        board = b;
        sound = s;
        guiHuman = h1;
        guiComputer = c1;
    }

    /*******************************************************
     * Method actionPerformed
     * 
     * Purpose: Determines coordinates for Computer Board 
     *          hits and decides if there is another round,
     *          if not prints the LinkedList
     * 
     * @param e - Captures action in buttons
     * 
     *******************************************************/
    @Override
    public void actionPerformed(ActionEvent e)
    {   

        Addons a = new Addons();
        Computer c = new Computer();
        Human h = new Human();
        int x = 0;
        int y = 0;
        // Which button?
        JButton button = (JButton)e.getSource();

        // Extract y and x from button Name which I set back in GuiBoard
        if(button.getName().length() < 4){
            y = Integer.parseInt(String.valueOf(button.getName().charAt(0)));
            x = Integer.parseInt(String.valueOf(button.getName().charAt(2)));
        }
        else if (Integer.parseInt(String.valueOf(button.getName().charAt(0))) >= board.length/2)
        {
            y = Integer.parseInt(String.valueOf(button.getName().charAt(0)));
            if(button.getName().length() <= 4){
                x = Integer.parseInt(String.valueOf(button.getName().charAt(2) 
                                    + String.valueOf(button.getName().charAt(3))));
            }
            else{
                x = Integer.parseInt(String.valueOf(button.getName().charAt(3)) 
                                   + String.valueOf(button.getName().charAt(4)));    
            }
        }
        else if (Integer.parseInt(String.valueOf(button.getName().charAt(0))) < board.length/2)
        {
            y = Integer.parseInt(String.valueOf(button.getName().charAt(0) 
                               + String.valueOf(button.getName().charAt(1))));
            if(button.getName().length() <= 4){
                x = Integer.parseInt(String.valueOf(button.getName().charAt(3)));
            }
            else{
                x = Integer.parseInt(String.valueOf(button.getName().charAt(3) 
                                   + String.valueOf(button.getName().charAt(4)))); 
            }
        }
        // Hit or Miss
        if(board[y][x] == 1)
        {
            sound.playExplo();
            JOptionPane.showMessageDialog(null, "You Hit!");
            board[y][x] = 3;
        }
        else if (board[y][x] == 0)
        {
            sound.playMiss();
            JOptionPane.showMessageDialog(null, "You Missed!");
            board[y][x] = 2;

        }
        // Call to make LinkedList
        h.play(board);

        //Is there a next round?
        if(a.nextRound(board))
        {
            a.sleep(1);
            c.play(board);
            guiComputer.dispose(); // Get rid of old board
            guiHuman.dispose(); // Get rid of old board
            guiHuman = new GuiBoard(board, 1);
            guiHuman.setVisible(true);

            guiComputer = new GuiBoard(board, sound, guiHuman, guiComputer);
            guiComputer.setDefaultCloseOperation(GuiBoard.DISPOSE_ON_CLOSE);
            guiComputer.setVisible(true); // show New board


        }
        else
        {
            guiComputer.dispose();// ^
            guiComputer = new GuiBoard(board, sound, guiHuman, guiComputer);
            guiComputer.setDefaultCloseOperation(GuiBoard.DISPOSE_ON_CLOSE);
            guiComputer.setVisible(true); // show New board
            h.print();
        }



    }    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.