首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX退出menuItem

JavaFX退出menuItem
EN

Stack Overflow用户
提问于 2018-09-22 12:22:52
回答 1查看 1.9K关注 0票数 1

如何通过单击菜单中的“退出”来关闭程序?我使用了场景生成器和FXML,所以我尝试在主类中创建一个closeOPT()方法,其中包含关闭我的应用程序的代码,但是当我在controler中创建一个实例时,它不起作用。所以现在,我不知道该怎么做。我还尝试将close方法与主类中的FXML id连接起来,但这也不起作用。

代码语言:javascript
复制
enter code here




public class GameController {

@FXML Button b1;
@FXML Button b2;
@FXML Button b3;
@FXML Button b4;
@FXML Button b5;
@FXML Button b6;
@FXML Button b7;
@FXML Button b8;
@FXML Button b9;
@FXML GridPane gameBoard;



private boolean isFirstPlayer = true;

public void buttonClickHandler(ActionEvent evt) {

    Button clickedButton = (Button) evt.getTarget();

    String buttonLabel = clickedButton.getText();

    if("".equals(buttonLabel) && isFirstPlayer) {
        clickedButton.setText("X");
        isFirstPlayer = false;
    }
    else if("".equals(buttonLabel)&& !isFirstPlayer) {
        clickedButton.setText("O");
        isFirstPlayer = true;
    }


    find3InARow();
}

private boolean find3InARow(){
       //Row 1
       if (""!=b1.getText() && b1.getText() == b2.getText() 
           && b2.getText() == b3.getText()){
           highlightWinningCombo(b1,b2,b3);
           return true;
       }
       //Row 2
       if (""!=b4.getText() && b4.getText() == b5.getText() 
           && b5.getText() == b6.getText()){
           highlightWinningCombo(b4,b5,b6);
           return true;
       }
       //Row 3
       if (""!=b7.getText() && b7.getText() == b8.getText() 
           && b8.getText() == b9.getText()){
           highlightWinningCombo(b7,b8,b9);
           return true;
       }
       //Column 1
       if (""!=b1.getText() && b1.getText() == b4.getText() 
           && b4.getText() == b7.getText()){
           highlightWinningCombo(b1,b4,b7);
           return true;
       }
       //Column 2
       if (""!=b2.getText() && b2.getText() == b5.getText() 
           && b5.getText() == b8.getText()){
           highlightWinningCombo(b2,b5,b8);
           return true;
       }
       //Column 3
       if (""!=b3.getText() && b3.getText() == b6.getText() 
           && b6.getText() == b9.getText()){
           highlightWinningCombo(b3,b6,b9);
           return true;
       }
       //Diagonal 1
       if (""!=b1.getText() && b1.getText() == b5.getText() 
           && b5.getText() == b9.getText()){
           highlightWinningCombo(b1,b5,b9);
           return true;
       }
       //Diagonal 2
       if (""!=b3.getText() && b3.getText() == b5.getText() 
           && b5.getText() == b7.getText()){
           highlightWinningCombo(b3,b5,b7);
           return true;
       }       
       return false;
   }

private void highlightWinningCombo(Button first, Button second, Button third){
       first.getStyleClass().add("winning-button");
       second.getStyleClass().add("winning-button");
       third.getStyleClass().add("winning-button");

   }

   public void menuClickHandler(ActionEvent evt){
        MenuItem clickedMenu = (MenuItem) evt.getTarget();
        String menuLabel = clickedMenu.getText();

        if ("Play".equals(menuLabel)){
            ObservableList<Node> buttons = 
                    gameBoard.getChildren();

            buttons.forEach(btn -> {
                ((Button) btn).setText("");
                 btn.getStyleClass().remove("winning-button");
            });

            isFirstPlayer = true;
        }
        if("Quit".equals(menuLabel)) {

        }   
   } 

 }


public class Main extends Application {

Stage primaryStage;

@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("KrizicKruzigIgra.fxml"));
        Scene scene = new Scene(root,300,320);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.show();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void closeOPT(Stage primaryStage){
    primaryStage.close();
}

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-22 12:52:42

如果希望能够在任何地方终止JavaFX应用程序,请使用Platform.exit()

导致JavaFX应用程序终止。如果在调用Application方法之后调用此方法,则JavaFX启动程序将调用Application方法并终止JavaFX应用程序线程。然后启动线程将关闭。如果没有其他正在运行的非守护进程线程,Java将退出。如果从预加载程序或Application方法调用此方法,则不可能调用Application方法。 可以从任何线程调用此方法。 注意:如果应用程序嵌入到浏览器中,则此方法可能没有任何效果。

另一种方法是关闭所有打开的窗口,只要Platform.isImplicitExit()返回true (参见Platform.setImplicitExit(boolean);看起来这可能是您最初试图做的事情。

代码语言:javascript
复制
if ("Quit".equals(menuLabel)) {
    // gameBoard is one of your @FXML annotated fields
    gameBoard.getScene().getWindow().hide();
}

只有当Window属于gameBoard是唯一打开的Window时,这才有效。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52456624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档