Hi guys I'm developing a board game and I am struggling to remove the buttons that i don't want on the panel.I have used an Array to create 64 buttons and some of the buttons the visibility is set to false to create a path.The game uses a counter and a roll die to move the object on the buttons.Now I want to remove this buttons because when i play the counter still counts the index of the hidden buttons.PLZ Help........
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Button_Start extends JFrame implements ActionListener
{
private JPanel btnpanel, controlpanel;
private JButton [] btn;
private JButton btnroll;
private Random random ;
private int sum ;
private final String [] letters = {"A","B","C","D","E","F","","G","H","I","J","K","L ","M","N","O","P","Q","R","S",
"T","U","V","W","X","Y","Z"};
public Button_Start()
{
super("Animal Rescue");
setSize(800 , 600);
setVisible(true);
loadButtons();
sum = 0;
}
public void loadButtons()
{
random = new Random();
btn = new JButton[64];
btnroll = new JButton("Roll");
btnpanel = new JPanel();
controlpanel = new JPanel();
controlpanel.setLayout(new GridLayout(2 ,5,5,5));
btnpanel.setLayout(new GridLayout(8 ,8,5,5));
controlpanel.add(btnroll);
for(int i = 0 ; i < btn.length ; i++)
{
int v = random.nextInt(25);
btn[i] = new JButton(letters[v]);
}
for(int k = 8; k < 15; k++)
btn[k].setVisible(false);
for(int i = 25; i < 32;i++)
btn[i].setVisible(false);
for(int l = 40; l < 47;l++)
btn[l].setVisible(false);
for(int z = 56; z < 64;z++)
btn[z].setVisible(false);
for(int i = 0; i < btn.length ; i++)
btnpanel.add(btn[i]);
btnroll.addActionListener(this);
add(btnpanel , BorderLayout.CENTER);
add(controlpanel , BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnroll)
{
ClearTiles();
int k = 1 + random.nextInt(6);
sum = sum + k;
JOptionPane.showMessageDialog(null,"The Value rolled is :"+k);
btn[sum -1].setBackground(Color.BLUE);
}
}
public void ClearTiles()
{
for(int i = 0 ; i < btn.length ; i++)
{
btn[i].setBackground(Color.LIGHT_GRAY);
}
}