0
我正在创建一个棋盘游戏,其中n个棋盘(本示例中为5 x 5)在每个单元格中包含随机字母。这些字母是从一个字符串数组中随机选择的,并有一个2d数组按钮,将它们放入JFrame中,随后是for循环,用于随机选择的字母(如字符串)的二维数组。下面的程序的目的是显示一个5×5的按钮板,这是不工作的。我将非常感谢任何帮助或指导解决这个问题。在JFrame板上镜像2d阵列
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);
}
}
(对不起任何代码的格式问题)
什么具体不工作?显示的字母?你能够更具体地了解你想要做什么以及目前发生了什么吗? –
只显示最后一个按钮,而不是全部25. –
您不能只取消注释frame.setLayout()行吗? –