Java Swing JFrame 打不开(扫雷游戏)?

标签 java swing jframe

我使用 Swing 用 Ja​​va 制作了一个扫雷游戏,但是当我运行它时,JFrame 没有弹出? (没有错误) 它运行,但没有窗口显示。尝试调试没有成功。

非常感谢您的帮助:)

请注意,在 GameBoard 类中:

imgs = new Image[13]; //Number of images used
    //Loading images, used later
    for (int i = 0; i < 13; i++) {
        imgs[i] = (new ImageIcon(i + ".png")).getImage();
    }

这些行加载由我创建的代表图像(13 个图像)的数字,这些图像的 loadid 取决于鼠标适配器。

这是具有 main 方法的 MineSweeper 类:

package minesweeper;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class MineSweeper extends JFrame {

public  JLabel label;

public MineSweeper(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(600, 600);
    this.setLocationRelativeTo(null);
    this.setTitle("Minesweeper");

    label = new JLabel("");
    this.add(label, BorderLayout.SOUTH);

    GameBoard game = new GameBoard(label);
    this.add(game);

    setResizable(false);

}


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {                
            MineSweeper jf = new MineSweeper();
            jf.setVisible(true);                
        }
    });
}




}

这是 GameBoard 类:

package minesweeper;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;



public class GameBoard extends JPanel {


//Adding constant variables
private final int SIZE_OF_CELL = 20;
private static final int NUM_OF_ROWS = 20;
private static final int NUM_OF_CL = 20;
private final int SIZE_OF_FIELD = NUM_OF_ROWS * NUM_OF_CL;
//Adding other variables
private int NUM_OF_MINES_LEFT;
private Image[] imgs;
private static int[][] gameboard;
public static int mines = 40;

private int all_cells;
private static JLabel label;
boolean gamestarted = true; // Game is in progress, already started

//Constructor 1 parameter
public GameBoard(JLabel inputlabel) {

    this.label = inputlabel;
    imgs = new Image[13]; //Number of images used
    //Loading images, used later
    for (int i = 0; i < 13; i++) {
        imgs[i] = (new ImageIcon(i + ".png")).getImage();
    }

    setDoubleBuffered(true);
    addMouseListener(new Manager());
    StartNewGame();
}


//Find mines around cell[i][j]
public static int FindMines(int ro, int co){
    int cnt=0;

    for(int y=-1;y<=1;y++){
        for(int x = -1; x<=1;x++){
            if(x==0 && y ==0) continue;
            if(ro+y<0) continue;
            if(co+x<0) continue;
            if(gameboard[ro+y][co+x]==19) cnt++; 
        }
    }
    return cnt;
}



public static void StartNewGame(){



    //NUM_OF_MINES_LEFT = 40; // Default value for number of mines is 30
    gameboard = new int[20][20];
    int minesleft=mines;
    int row,col;
   // int mines = NUM_OF_MINES_LEFT;
    Random rng=new Random();
    label.setText(Integer.toString(minesleft));

    //initialize mine field
    for(int i=0;i<20;i++){
        for (int j=0;j<20;j++){
            gameboard[i][j]=10;//default value is 10 -> its covered
        }
    }

   //Set mines in random positions 
    while (mines>0){                          
        row=rng.nextInt(NUM_OF_ROWS);
        col=rng.nextInt(NUM_OF_CL);
        if ((gameboard[row][col])!=19){
            gameboard[row][col]+=9;;
            minesleft--;
        }
    }



    //Set numbers
    for(int i=0;i<20;i++){
        for (int j=0;j<20;j++){

            if(gameboard[i][j]==19) gameboard[i][j]=19; 

            if(gameboard[i][j]==10){
                gameboard[i][j]+= FindMines(i,j);
            }
        }
    }

 }



   //public int FindEmptyCells(){



  //}
  @Override
  public void paintComponent(Graphics grap){

    int gamewon=0;
    int[][] temp = new int[20][20];

    for(int i=0;i<20;i++){
        for(int j=0;j<20;j++){

            temp[i][j]=gameboard[i][j];

            if(gamestarted && temp[i][j]==9) gamestarted=false;

            if(gamestarted == false){
                if(temp[i][j]==19){
                    temp[i][j]= 9;
                }else if(temp[i][j]==29){//10+11 for mark
                    temp[i][j]=11;
                }else if(temp[i][j]>9){
                    temp[i][j]=10;
                }
            }else{
                if (temp[i][j] > 19)
                    temp[i][j] = 11;
                else if (temp[i][j] > 9) {
                    temp[i][j] = 10;
                    gamewon=1;
                }
            }
            int toload= temp[i][j];
            grap.drawImage(imgs[toload],j*15,i*15,this);


        }
    }

    if(gamestarted==true && gamewon==0){
        gamestarted=false;
        label.setText("You won!");
    }else if(gamestarted==false){
        label.setText("You Lost!");
    }
}

class Manager extends MouseAdapter{

@Override   
public void mousePressed(MouseEvent ev){


    boolean newpaint = false;

    //Get event coordinates
    int x= ev.getX();
    int y= ev.getY();
    int hit_cl= x / 15;
    int hit_row= y/ 15;

    if(gamestarted==false){
        StartNewGame();
        repaint();
    }


    if( (x < 20 * 15) && (y < 20 * 15) ){

        if(ev.getButton() ==  MouseEvent.BUTTON3){

            if(gameboard[hit_cl][hit_row] > 9){
                newpaint=true;

                if(gameboard[hit_cl][hit_row] <= 19){
                    if(mines > 0){
                        mines--;
                        String show=Integer.toString(mines);
                        gameboard[hit_cl][hit_row]+=11;
                        label.setText(show);

                    }else{
                        label.setText("Marks: 0");
                    }
                }else{
                    mines++;
                    String show=Integer.toString(mines);
                    label.setText(show);
                    gameboard[hit_cl][hit_row]-=11;
                }

            }


        }else{
        if(gameboard[hit_cl][hit_row] > 19){
            return;
        }
            if((gameboard[hit_cl][hit_row] > 9) && (gameboard[hit_cl]   
  [hit_row] <29)){
                newpaint=true;
                gameboard[hit_cl][hit_row]-=10;

            if(gameboard[hit_cl][hit_row] == 9) gamestarted=false;
            //if(gameboard[hit_cl][hit_row] == 10); //find_empty();


        }
    }
    if(newpaint==true) repaint();
}

}   

}

}

最佳答案

你做了什么调试? :)

程序永远运行的最明显原因是它永远不会离开循环。

while (mines>0){                          
        row=rng.nextInt(NUM_OF_ROWS);
        col=rng.nextInt(NUM_OF_CL);
        if ((gameboard[row][col])!=19){
            gameboard[row][col]+=9;;
            minesleft--;
        }
    }

StartNewGame() 方法(GameBoard 类)的这一部分导致无限循环,mines 变量永远不会在循环内更新。

快速查看您的代码,我可以注意到一些不好的做法,例如:

  • 您不应该控制游戏状态或在其中设置标签文本 PaintComponent() 方法。该方法会被自动调用,并且它 应该绘制所有组件。所有的“逻辑”都在里面 可能会减慢你的程序,因为你无法控制有多少 该方法被调用的次数(当然你可以强制该方法 例如,使用 repaint() 方法调用)。
  • 您应该关注java naming conventions

希望这有帮助:)

关于Java Swing JFrame 打不开(扫雷游戏)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40695162/

相关文章:

java - 在一个 try 语句中打开多个资源不可靠吗?为什么?

java - JPanel 设置为不可见,除默认设置之外的组合框选择将其设置为可见,但缺少组件

java - 为什么我的 JButtons 在小程序调整大小之前不会出现,而我的 JCombo 框在单击之前是不可见的?

java - 在 JFrame 上居中 JLabel

java - 用 Java 放映幻灯片?

java - 为什么 "fileInputStream.read()"的值会改变呢?

java - 如何在 Java 中使用泛型实现工厂模式?

java - 如何将多个参数传递给java反射中的方法

Java SE框架和架构?

java - 如何根据JFrame大小设置JScrollPane的大小?