Java中的JFrame在什么条件下是空白的

Java中的JFrame在什么条件下是空白的,java,swing,jframe,Java,Swing,Jframe,我正在尝试Java中的JFrames,每当我运行程序时,就会出现一个空窗口。我添加了我的按钮,我的面板,但它是完全空白的。关于JFrame,我是否应该知道可能导致此错误的任何具体信息?最终产品根本没有按钮。我已经检查了每个方法是否都被调用了 作为澄清,我不是要求修改代码,因为我的代码太长,我不知道如何简化它,也不知道如何对问题进行简化,以便提供一个示例我发帖的原因是询问JFrame的性质,以及在什么情况下它会是这样的一个空屏幕。我还查看了如何简化代码的帖子,但它似乎不适用(如果我错了,请纠正我)

我正在尝试Java中的JFrames,每当我运行程序时,就会出现一个空窗口。我添加了我的按钮,我的面板,但它是完全空白的。关于JFrame,我是否应该知道可能导致此错误的任何具体信息?最终产品根本没有按钮。我已经检查了每个方法是否都被调用了

作为澄清,我不是要求修改代码,因为我的代码太长,我不知道如何简化它,也不知道如何对问题进行简化,以便提供一个示例我发帖的原因是询问JFrame的性质,以及在什么情况下它会是这样的一个空屏幕。我还查看了如何简化代码的帖子,但它似乎不适用(如果我错了,请纠正我),因为我的问题是缺少投票率,因此,逐位删除代码不会导致错误消失

完整代码:

    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;
}
公共类扫雷服务扩展JFrame{
私有JButton[]按钮;
私人JPanel小组;
私有字符串[][]网格;
私人内部高度;
私有整数宽度;
私有JButton flagButton;
私有布尔标志;
私有JLabel结果;
公共扫雷服务(国际高度2,国际宽度2,国际炸弹){
高度=高度2;
宽度=宽度2;
按钮=新按钮[高度*宽度];
flag=false;
结果=新的JLabel(“播放”);
网格=新字符串[高度][宽度];
for(int i=0;i=0&&x-1>=0&&grid[y-1][x-1]!=null&&grid[y-1][x-1]。子字符串(1,2)。等于(“F”)){
网格[y-1][x-1]=''+网格[y-1][x-1]。子字符串(0,1)+“R”;
}
如果(y-1>=0&&grid[y-1][x]!=null&&grid[y-1][x]。子串(1,2)。等于(“F”)){
格点[y-1][x]=“”+格点[y-1][x]。子串(0,1)+“R”;
}
如果(y-1>=0&&x+1=0&&grid[y][x-1]!=null&&grid[y][x-1]。子串(1,2)。等于(“F”)){
格点[y][x-1]=“”+格点[y][x-1]。子串(0,1)+“R”;
}
如果(x+1=0&&grid[y+1][x-1]!=null&&grid[y+1][x-1]。子串(1,2)。等于(“F”)){
格点[y+1][x-1]=“”+格点[y+1][x-1]。子串(0,1)+“R”;
}
如果(y+1public 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 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 y = 0; y < height; y++) { 
        for (int x = 0; x < width; x++) { 
            grid[x][y] = "AA";
        }
    }
    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();

    add(panel);
    pack();
    //setSize(20 * width, 20 * height);
    setLocationRelativeTo(null);
}