简体   繁体   中英

New Game Button Causes Java Game Application To Stop Responding

I'm making a java poker application using javaFX. I'm relatively new to java and javaFX in particular. I don't seem to be getting any explicit errors when my code compiles but when I run the program and click button "New Game" button, my application stops responding and I have no clue as to why.

The Opponent class is an abstract interface and inside this class there are four classes that describe the type of opponents.

The Opponent class is declared as:

public abstract interface Opponent

and one type of opponent is written as:

public static class tightAggressiveOpponent extends Player
        implements Opponent

I have tried to resolve this issue before posting my question. I would be grateful if anyone could offer any suggestions? The code for the actual button itself is:

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);
    }

It looks like you have an infinite loop or a blocking operation inside the method playGame .

You should run such method in a background thread. For example:

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

Also you cannot/should not update UI from a background thread. So use code like below inside the playGame method to update UI:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM