I am creating a board game where an n by n board (5 x 5 in this example) contains random letters in each cell. The letters are selected randomly from an array of strings and there is a 2d array of buttons to place them into the JFrame following a for loop for the 2d array of randomly selected letters (as Strings). The intent of the program below is to display a 5 by 5 board of buttons, which is not working. I would be very grateful for any help or instruction on fixing this problem.
package com.content;
import javax.swing.*;
import java.awt.Dimension;
public class WordDisplay {
static int random;
static JFrame frame = new JFrame("WordMatch");
public static void newFrame() {
frame.setSize(new Dimension(800, 600));
//frame.setLayout(new GridLayout(3, 2));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(false);
}
public static void fillFrame(){
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"};
JButton[][] buttons = new JButton[5][5];
for (int r = 0;r<5;r++){
for (int c = 0;c<5;c++){
buttons[r][c] = new JButton("");
}
}
String[][] Board = new String[5][5];
for (int r = 0;r<5;r++){
for (int c = 0;c<5;c++){
random = (int)(Math.random() * 25);
Board[r][c] = Letters[random];
buttons[r][c].setText(Board[r][c]);
System.out.print(Board[r][c]);
frame.add(buttons[r][c]);
}
}
}
public static void main(String args[]){
newFrame();
fillFrame();
frame.setVisible(true);
}
}
(Sorry for any code formatting issues)
Basically, when you have a 2D array and you want to mirror it, what you should do is basically this:
for(int x=0;x<array.length;x++){
for(int y=0;y<array[x].length;y++){
array[x][y]=array[(array[x].length)-x][y]
}
}
If you could be more specific with the problem of your code, I should be able to be more helpfull...