如何让方法运行一定次数

时间:2016-08-03 02:39:38

标签: java swing methods jpanel

我在java中制作了一款游戏,但它只运行了一轮。我希望它能够根据拨号盒中按下的数字运行一定次数。我认为答案是在一场比赛结束后运行该方法,虽然我不确定如何做到这一点。

创建jframe和方法

public Rockgame() {

    // JFrame
    p = new ImagePanel(Toolkit.getDefaultToolkit().getImage("space.jpg"));
    f = new JFrame("SpaceMiners");

    f.setSize(700, 500);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

确定运行次数的按钮:

Object[] options = { "Three", "Five", "Ten" };
    no_of_games = (int) JOptionPane.showOptionDialog(f,
            "Would you like to play best of:", "Rounds" + "",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
            null, options, options[2]);

if (no_of_games == 0) {
        no_of_games = no_of_games + 3;
    }
    if (no_of_games == 1) {
        no_of_games = no_of_games + 4;
    }
    if (no_of_games == 2) {
        no_of_games = no_of_games + 8;
    }

创建网格和鼠标滑块

p.setLayout(new GridLayout(10, 10));
    f.getContentPane().add(p, BorderLayout.CENTER);

    for (int x = 0; x < 10; x++) {

        for (int y = 0; y < 10; y++) {

            playingGrid[x][y] = new JLabel(new ImageIcon("rock.png"));
            p.add(playingGrid[x][y]);

            playingGrid[x][y].addMouseListener(new Rockbreaker());

        }
    }

}

public class Rockbreaker implements MouseListener {
    // manbitesdog6
    public void mouseClicked(MouseEvent e) {
        // sets all columns greater than one clicked to invisible

            if (e.getSource() == playingGrid[0][0]){
                for(int u =0;u<10;u++){
                for(int y =0;y<10;y++){
                Rockgame();

                }
            }
            }

            for (int x = 0; x < 10; x++) {
                for (int y = 0; y < 10; y++) {
                    if (playingGrid[x][y] == e.getSource()) {
                        for (int k = 0; k < 10; k++) {
                            for (int i = 0; i < 10; i++) {
                                if ((i >= x) && (k >= y)) {
                                    playingGrid[i][k].setVisible(false);
                                }
                            }
                        }

                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

尝试使用number_format($var, 8); - 循环。

number_format

另外,请看一下:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html