【问题标题】:New Game Button Causes Java Game Application To Stop Responding新游戏按钮导致 Java 游戏应用程序停止响应
【发布时间】:2017-10-28 11:39:24
【问题描述】:

我正在使用 javaFX 制作一个 java 扑克应用程序。我对 java 和 javaFX 比较陌生。当我的代码编译时,我似乎没有得到任何明确的错误,但是当我运行程序并单击按钮“新游戏”按钮时,我的应用程序停止响应,我不知道为什么。

Opponent 类是一个抽象接口,在这个类中有四个描述对手类型的类。

Opponent 类声明为:

public abstract interface Opponent

一种类型的对手写成:

public static class tightAggressiveOpponent extends Player
        implements Opponent

在发布我的问题之前,我已尝试解决此问题。如果有人可以提供任何建议,我将不胜感激?实际按钮本身的代码是:

public void handleNewGameButtonAction(ActionEvent event) {
       run();
    }

public void run()
    {
        this.gameInfoText.set("Run method called");
        Table table = new Table();
        Deck deck = new Deck();
        Pot pot = new Pot();

        player1 = new Player("Oliver", false, pot, Boolean.valueOf(true));
        Opponent.tightAggressiveOpponent opponent = new Opponent.tightAggressiveOpponent("Jeremy (TA)", true, pot, table);
        player1.setOpponentPlayStyle(opponent.getPlayStyle());
        opponent.setOpponent(player1);

        playGame(player1, opponent, table, deck, pot);
    }

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    看起来您在方法playGame 中有一个无限循环或阻塞操作。

    您应该在后台线程中运行此类方法。例如:

    new Thread(new Runnable(){
        public void run(){
            playGame(player1, opponent, table, deck, pot);
        }
    }).start();
    

    此外,您不能/不应该从后台线程更新 UI。因此,请在 playGame 方法中使用如下代码来更新 UI:

    Platform.runLater(new Runnable(){
        public void run(){
            textArea.setText("something");
        }
    });
    

    【讨论】:

    • 感谢您的回复,纳宾。这个建议会完全取代原来的运行方法吗?我之前没有使用过 Thread 类。
    • 可以原样放置原来的run方法。只需将代码的最后一条语句替换为我回答中的上层代码块即可。
    • 对不起,我的意思是补充一点,这段代码是在 fxml Controller 类中找到的。
    • 那又怎样?去做吧。
    • 最后一部分是我放的时候报错 player1 = new Player("Oliver", false, pot, Boolean.valueOf(true)); Opponent.tightAggressiveOpponent 对手 = new Opponent.tightAggressiveOpponent("Jeremy (TA)", true, pot, table); player1.setOpponentPlayStyle(opponent.getPlayStyle());对手.setOpponent(player1);新线程(新可运行(公共无效运行(){ playGame(玩家1,对手,桌子,甲板,锅);}))。开始(); }
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    相关资源
    最近更新 更多