我正在尝试用Java编写JFrames,每当我运行程序时,会出现一个空窗口。我添加了我的按钮,我的面板,但它完全是空白的。是否有任何关于JFrame的具体内容我应该知道可能导致这个错误?最终产品根本没有按钮。我已经检查过每个方法都已被调用。
作为澄清,我不是要求进行代码修改,因为我的代码太长而且我不知道如何简化它,也不知道这个问题是否足够,为了提供一个例子。 我发帖的原因是询问JFrame的性质以及在什么条件下这样的空白屏幕。我还查看了如何简化代码的帖子,它没有&#39 ; t似乎适用(纠正我,如果我错了)因为我的问题是缺乏道岔,所以逐位删除代码不会导致错误消失。
完整代码:
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;
}
}
答案 0 :(得分:4)
关于JFrame的性质以及在什么条件下它会像这样的空屏幕
revalidate
(或其他子容器)上调用repaint
和contentPane
之前,您已将窗口显示为revalidate
和repaint
进行一系列猜测
根据现有资料......
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);
}
panel
永远不会添加到框架
因此,在进行了一些修改以使代码运行之后,我将panel
添加到contentPane
的{{1}},而不是JFrame
, setSize
(因为它没有在我的电脑上正确布局)
pack
这表明您可能需要仔细查看Laying Out Components Within a Container
答案 1 :(得分:0)
您从未将面板添加到相框中。由于您已经扩展了JPanel,只需在框架可见之前设置所有内容,然后将面板添加到框架中。否则,您还创建了一个JFrame,这是必要的,因为您已经扩展了它,删除其中一个,然后执行相同的过程。你的代码应该在那之后工作。