I am attempting my first go at JFrames in Java, and whenever I run my program an empty window shows up. I have added my buttons, my panel, yet it is completely blank. Is there anything specific about JFrame that I should know that could cause this mistake? The final product has no buttons at all. I have checked to see that every method has been called.
As a clarification, I am not asking for code revisions, as my code is too long and I don't know how to simplify it, nor do I know enough about the problem to do so, in order to provide an example. The reason I am posting is to ask about the nature of JFrame and under what conditions it would be an empty screen like this. I also looked at the post for how to simplify code, and it doesn't seem to apply(correct me if I'm wrong) because my problem is a lack of turnout, so removing code bit-by-bit will not result in the error disappearing.
Full Code :
public class MineSweeperVisual extends JFrame{
private JButton[] buttons;
private JPanel panel;
private String[][] grid;
private int height;
private int width;
private JButton flagButton;
private boolean flag;
private JLabel result;
public MineSweeperVisual(int height2, int width2, int bombs) {
height = height2;
width = width2;
buttons = new JButton[height * width];
flag = false;
result = new JLabel("Playing");
grid = new String[height][width];
for (int i = 0; i < bombs; i++) {
int x = (int) (Math.random()*width);
int y = (int) (Math.random()*height);
grid[y][x] = "BH";
}
grid = resetGrid(grid, height, width);
loadButtons();
createFlagButton();
createPanel();
setSize(20 * width, 20 * height);
setLocationRelativeTo(null);
}
public void createPanel() {
panel = new JPanel();
for(JButton i: buttons)
panel.add(i);
panel.add(flagButton);
panel.add(result);
setBackground(Color.BLACK);
}
public void loadButtons() {
for (int i = 0; i < height * width; i ++) {
buttons[i] = createGameButton(i);
}
}
public JButton createGameButton(int i) {
JButton button = new JButton(grid[i / width][i % height].substring(0,1));
button.setPreferredSize(new Dimension(20,20));
int y = i / 10;
int x = i % 10;
class RevealListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (flag) {
grid[y][x] = grid[y][x].substring(0,1) + "F";
} else {
grid[y][x] = grid[y][x].substring(0,1) + "R";
if (grid[y][x].substring(0,1).equals("B")) {
result.setText("You lost due to explosion");
}
grid = zeroChange(grid, height, width, x, y);
for (int y1 = 0; y1 < height; y1 ++) {
for (int x1 = 0; x1 < width; x1++) {
if (grid[y1][x1].substring(0,2).equals("0R")) {
grid = adjacentToZeroChange(grid, height, width, x1, y1);
}
}
}
}
}
}
ActionListener listener = new RevealListener();
button.addActionListener(listener);
return button;
}
private void createFlagButton () {
flagButton = new JButton("Flag Mode");
class FlagListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (!flag)
flag = true;
else
flag = false;
}
}
ActionListener listener = new FlagListener();
flagButton.addActionListener(listener);
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.print("Enter the first dimension of the grid: ");
int width = in.nextInt();
System.out.print("Enter the second dimension of the grid: ");
int height = in.nextInt();
System.out.print("Enter the amount of bombs on the grid: ");
int bombs = in.nextInt();
JFrame frame = new MineSweeperVisual(height, width, bombs);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}public static String[][] adjacentToZeroChange(String[][] grid1, int height, int width, int x, int y) {
String[][] grid = grid1;
if (grid[y][x].substring(0,1).equals("0")) {
if (y - 1 >= 0 && x - 1 >= 0 && grid[y - 1][x - 1] != null && !grid[y - 1][x - 1].substring(1,2).equals("F")) {
grid[y - 1][x - 1] = "" + grid[y - 1][x - 1].substring(0,1) + "R";
}
if (y - 1 >= 0 && grid[y - 1][x] != null && !grid[y - 1][x].substring(1,2).equals("F")) {
grid[y - 1][x] = "" + grid[y - 1][x].substring(0,1) + "R";
}
if (y - 1 >= 0 && x + 1 < width && grid[y - 1][x + 1] != null && !grid[y - 1][x + 1].substring(1,2).equals("F")) {
grid[y - 1][x + 1] = "" + grid[y - 1][x + 1].substring(0,1) + "R";
}
if (x - 1 >= 0 && grid[y][x - 1] != null && !grid[y][x - 1].substring(1,2).equals("F")) {
grid[y][x - 1] = "" + grid[y][x - 1].substring(0,1) + "R";
}
if (x + 1 < width && grid[y][x + 1] != null && !grid[y][x + 1].substring(1,2).equals("F")) {
grid[y][x + 1] = "" + grid[y][x + 1].substring(0,1) + "R";
}
if (y + 1 < height && x - 1 >= 0 && grid[y + 1][x - 1] != null && !grid[y + 1][x - 1].substring(1,2).equals("F")) {
grid[y + 1][x - 1] = "" + grid[y + 1][x - 1].substring(0,1) + "R";
}
if (y + 1 < height && grid[y + 1][x] != null && !grid[y + 1][x].substring(1,2).equals("F")) {
grid[y + 1][x] = "" + grid[y + 1][x].substring(0,1) + "R";
}
if (y + 1 < height && x + 1 < width && grid[y + 1][x + 1] != null && !grid[y + 1][x + 1].substring(1,2).equals("F")) {
grid[y + 1][x + 1] = "" + grid[y + 1][x + 1].substring(0,1) + "R";
}
}
return grid;
}
public static String[][] zeroChange(String[][] grid1, int height, int width, int x, int y) {
String[][] grid = grid1;
if (y - 1 >= 0 && x - 1 >= 0 && grid[y - 1][x - 1] != null && grid[y - 1][x - 1].substring(0,2).equals("0H")) {
grid[y - 1][x - 1] = "0R";
grid = zeroChange(grid, height, width, y -1, x -1);
}
if (y - 1 >= 0 && grid[y - 1][x] != null && grid[y - 1][x].substring(0,2).equals("0H")) {
grid[y - 1][x] = "0R";
grid = zeroChange(grid, height, width, y -1, x);
}
if (y - 1 >= 0 && x + 1 < width && grid[y - 1][x + 1] != null && grid[y - 1][x + 1].substring(0,2).equals("0H")) {
grid[y - 1][x + 1] = "0R";
grid = zeroChange(grid, height, width, y -1, x +1);
}
if (x - 1 >= 0 && grid[y][x - 1] != null && grid[y][x - 1].substring(0,2).equals("0H")) {
grid[y][x - 1] = "0R";
grid = zeroChange(grid, height, width, y, x -1);
}
if (x + 1 < width && grid[y][x + 1] != null && grid[y][x + 1].substring(0,2).equals("0H")) {
grid[y][x + 1] = "0R";
grid = zeroChange(grid, height, width, y, x +1);
}
if (y + 1 < height && x - 1 >= 0 && grid[y + 1][x - 1] != null && grid[y + 1][x - 1].substring(0,2).equals("0H")) {
grid[y + 1][x - 1] = "0R";
grid = zeroChange(grid, height, width, y +1, x -1);
}
if (y + 1 < height && grid[y + 1][x] != null && grid[y + 1][x].substring(0,2).equals("0H")) {
grid[y + 1][x] = "0R";
grid = zeroChange(grid, height, width, y +1, x);
}
if (y + 1 < height && x + 1 < width && grid[y + 1][x + 1] != null && grid[y + 1][x + 1].substring(0,2).equals("0H")) {
grid[y + 1][x + 1] = "0R";
grid = zeroChange(grid, height, width, y +1, x +1);
}
if (grid[y][x].substring(0,1).equals("0")) {
grid = adjacentToZeroChange(grid, height, width, x, y);
}
return grid;
}
public static int spotCheck(String[][] grid, int x, int y, int width, int height) {
int bAmount = 0;
if (y - 1 >= 0 && x - 1 >= 0 && grid[y - 1][x - 1] != null && grid[y - 1][x - 1].substring(0,1).equals("B")) {
bAmount++;
}
if (y - 1 >= 0 && grid[y - 1][x] != null && grid[y - 1][x].substring(0,1).equals("B")) {
bAmount++;
}
if (y - 1 >= 0 && x + 1 < width && grid[y - 1][x + 1] != null && grid[y - 1][x + 1].substring(0,1).equals("B")) {
bAmount++;
}
if (x - 1 >= 0 && grid[y][x - 1] != null && grid[y][x - 1].substring(0,1).equals("B")) {
bAmount++;
}
if (x + 1 < width && grid[y][x + 1] != null && grid[y][x + 1].substring(0,1).equals("B")) {
bAmount++;
}
if (y + 1 < height && x - 1 >= 0 && grid[y + 1][x - 1] != null && grid[y + 1][x - 1].substring(0,1).equals("B")) {
bAmount++;
}
if (y + 1 < height && grid[y + 1][x] != null && grid[y + 1][x].substring(0,1).equals("B")) {
bAmount++;
}
if (y + 1 < height && x + 1 < width && grid[y + 1][x + 1] != null && grid[y + 1][x + 1].substring(0,1).equals("B")) {
bAmount++;
}
return bAmount;
}
public static String[][] resetGrid(String[][] grid, int height, int width) {
String[][] change = grid;
for (int y = 0; y < height; y ++) {
for (int x = 0; x < width; x ++) {
if (change[y][x] != null && change[y][x].substring(0,1).equals("B"))
continue;
else {
change[y][x] = "" + spotCheck(change, x, y, width, height) + "H";
}
}
}
return change;
}
}