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();
}
}
}
}