当前位置: 首页 > 知识库问答 >
问题:

关闭应用程序后程序不停止

锺离晗昱
2023-03-14

我试图重新创建Connect四,我成功了。但我想通过频繁地切换颜色,给玩家一个获胜的四张光盘在哪里的指示。我对线程和编程中的时间概念是新的。

我也成功地给了用户这个指示,但是在关闭应用程序之后,控制台仍然会给出输出,也是在使用SetonCloserEquest时。

代码如下:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class FourInARow extends Application {

GridPane boardGrid = new GridPane();

Label[][] labels = new Label[7][7];
Label statusLabel = new Label();
int[][] cell = new int[7][6];

int player = 0;
int won = 0;

String baseStyle = "-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: ";

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

public static void main(String[] args){launch(args);}

private void init(Stage window){

    createLabels();
    startGame();

    Label above = new Label("Try to connect four discs in a row!");
    above.setStyle("-fx-font-size: 30; -fx-alignment: center; -fx-min-width: 600");
    boardGrid.setStyle("-fx-background-color: silver;-fx-border-color: #F4F4F4;-fx-border-width: 0 20 0 20");
    Button newGame = new Button("New Game");
    newGame.setStyle("-fx-min-width: 100;-fx-font-size:20");
    newGame.setOnAction(e -> startGame());
    statusLabel.setStyle("-fx-font-size: 30;-fx-alignment: center; -fx-min-width: 300;");
    HBox below = new HBox();
    below.setStyle("-fx-border-width: 0 0 0 20;-fx-border-color: #F4F4F4");
    below.getChildren().addAll(newGame, statusLabel);
    VBox layout = new VBox();
    layout.getChildren().addAll(above, boardGrid, below);
    Scene scene = new Scene(layout, 600, 620);
    scene.setOnKeyPressed(e -> {
        if (won == 0) {
            try {
                String k = e.getCode().toString();
                int l = k.length();
                int col = Integer.parseInt(k.substring(l - 1, l)) - 1;
                placeDisc(col, player);
                switchPlayer();
                updateScreen();
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException error) {
                System.out.println("error: " + error);
            }
        }
    });
    window.setScene(scene);
    window.setTitle("Connect Four");

    threadThing();
}

private void threadThing() {
    service.scheduleAtFixedRate(() -> {
        try {
            wonStyle();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }, 0, 1, TimeUnit.SECONDS);
}

private void startGame() {
    cell = new int[7][6];
    won = player = 0;
    statusLabel.setText("");
    updateScreen();
}

private void updateScreen() {
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            labels[i][j].setStyle(baseStyle + addStyle(cell[i][j]));
        }
        labels[i][6].setText(Integer.toString(i+1));
        labels[i][6].setStyle("-fx-alignment: center;-fx-min-width: 80;-fx-background-color: #F4F4F4;-fx-font-size: 30;");
    }

    switch(won) {
        case 1: statusLabel.setText("Blue has won!");break;
        case 2: statusLabel.setText("Yellow has won!");break;
    }
}

private String addStyle(int cell) {
    String style = "silver";
    switch(cell){
        case 1: style = "blue"; break;
        case 2: style = "yellow"; break;
        case 3: style = "darkblue"; break;
        case 4: style = "gold;"; break;
    }
    return style;
}

private void placeDisc(int col, int player) {
    for (int i = 5; i >= 0 ; i--) {
        if(cell[col][i] == 0){
            cell[col][i] = 1;
            if(player == 1) cell[col][i] = 2;
            break;
        }else{
            if(i==0) switchPlayer();
        }
    }
    checkWon();
}

private void checkWon() {
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            if (cell[i][j] != 0) {
                try {
                    if (cell[i][j] == cell[i][j + 1] && cell[i][j] == cell[i][j + 2] && cell[i][j] == cell[i][j + 3]) {
                        won = cell[i][j];
                        cell[i][j] = cell[i][j + 1] = cell[i][j + 2] = cell[i][j + 3] = cell[i][j] + 2;
                    }
                }catch(ArrayIndexOutOfBoundsException error) {}
                try {
                    if (cell[i][j] == cell[i + 1][j] && cell[i][j] == cell[i + 2][j] && cell[i][j] == cell[i + 3][j]) {
                        System.out.println("Horizontal win");
                        won = cell[i][j];
                        cell[i][j] = cell[i + 1][j] = cell[i + 2][j] = cell[i + 3][j] = cell[i][j] + 2;
                    }
                }catch(ArrayIndexOutOfBoundsException error) {}
                try {
                    if (cell[i][j] == cell[i + 1][j + 1] && cell[i][j] == cell[i + 2][j + 2] && cell[i][j] == cell[i + 3][j + 3]) {
                        won = cell[i][j];
                        cell[i][j] = cell[i + 1][j + 1] = cell[i + 2][j + 2] = cell[i + 3][j + 3] = cell[i][j] + 2;
                    }
                }catch(ArrayIndexOutOfBoundsException error) {}
                try {
                    if (cell[i][j] == cell [i + 1][j - 1] && cell[i][j] == cell[i + 2][j - 2] && cell[i][j] == cell[i + 3][j - 3]) {
                        won = cell[i][j];
                        cell[i][j] = cell[i + 1][j - 1] = cell[i + 2][j - 2] = cell[i + 3][j - 3] = cell[i][j] + 2;
                    }
                }catch(ArrayIndexOutOfBoundsException error) {}
            }
        }
    }
}

private void switchPlayer() {
    if(player == 0) player = 2;
    player--;
}

private void createLabels() {
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            labels[i][j] = new Label();
            boardGrid.add(labels[i][j], i, j);
        }
    }
}

private void wonStyle() throws InterruptedException {
    System.out.println("Test");
    boolean run = false;
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            if(cell[i][j] > 2 && !run){
                Thread.sleep(500);
                addStyleFlicker();
                run = true;
            }
        }
    }
}

private void addStyleFlicker() throws InterruptedException {
    String[] styleOne = {"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: blue;",
                         "-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: darkblue;"};
    String[] styleTwo = {"-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: yellow;",
                         "-fx-background-radius: 40; -fx-min-width: 80; -fx-min-height: 80; -fx-alignment: center; -fx-border-width: 2; -fx-border-color: #000000;-fx-background-color: gold;"};
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            if(cell[i][j] == 3){
                labels[i][j].setStyle(styleOne[0]);
            }else if(cell[i][j] == 4){
                labels[i][j].setStyle(styleTwo[0]);
            }
        }
    }
    Thread.sleep(500);
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            if(cell[i][j] == 3){
                labels[i][j].setStyle(styleOne[1]);
            }else if(cell[i][j] == 4) {
                labels[i][j].setStyle(styleTwo[1]);
            }
        }
    }
}

@Override
public void start(Stage window) throws Exception {
    init(window);
    window.show();
}
}

共有1个答案

贺俊楚
2023-03-14

从Application类重写stop()方法将允许您关闭控制台应用程序:

@Override
public void stop() {
    System.exit(0);
}
 类似资料:
  • 我正在用场景构建器创建一个JavaFX应用程序。我在开头加了一段视频。所以我想播放视频之前,我的应用程序启动在全屏模式。问题是当它停止时,我只看到黑色尖叫,什么也没发生,我想这是因为视频是全屏的,它没有自动关闭。 我也有一个bug,在视频开始之前,我的主窗口的一些闪烁。我想这是因为视频放在控制器中,在我的应用程序启动后开始。 公共类主扩展应用程序{ 控制器类:

  • 编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答问题。 我的主要活动: 我的清单: 我的下一个代码: 下一个: 对于堆栈跟踪排序: 我的xml文件显示第一页: 要显示第二个选项卡的我的xml文件: 最后我想告诉你,我已经坚持了三天了。请更正此代码。中的“TabListener”接口不可用。但忽略它,在几毫秒后运行我的应用程序,然后“很遗憾,你的应用程序停止工作

  • 启动 1. 轻触主画面上您想启动的应用程序图标。 显示LiveArea™。 2. 轻触[开始]。 中断/继续 按下PS键即可返回LiveArea™。若要继续,请轻触[继续]。 关闭 1. 按下PS键。 返回LiveArea™。 2. 请由画面右上角将LiveArea™撕下。

  • 当用户使用后退按钮退出应用程序时,Android中的后台服务将停止运行。如果应用程序在前台或后台(点击HOME按钮),同样的服务工作得很好。 有3个案例: null MainActivity.java SimpleService

  • 如何在应用程序关闭后保存arraylist。这是我的环球班 这是我的旅行班 我想保存数组列表,当我关闭和打开应用程序的数组列表(所有旅行)被保存。如何做到这一点? 我想使用共享首选项,但我不知道如何做到这一点,因为它不是带有Trip的字符串it数组列表。有人能帮我在共享首选项中保存arraylist吗?