【问题标题】:JavaFX Quit menuItemJavaFX 退出菜单项
【发布时间】:2018-09-22 21:04:54
【问题描述】:

如何通过单击菜单中的“退出”来关闭我的程序? 我正在使用 Scene Builder 和 FXML,所以我尝试在主类中创建一个方法 closeOPT(),其中包含用于关闭我的应用程序的代码,但是当我在控制器中创建一个实例时,它不起作用。所以现在,我不知道如何使它工作。 我还尝试在主类中将 close 方法与 FXML id 连接,但这也没有用。

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

【问题讨论】:

    标签: java javafx


    【解决方案1】:

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

    导致 JavaFX 应用程序终止。如果在调用 Application start 方法之后调用此方法,则 JavaFX 启动器将调用 Application stop 方法并终止 JavaFX 应用程序线程。然后启动器线程将关闭。如果没有其他非守护线程正在运行,Java VM 将退出。如果从 Preloader 或 Application init 方法调用此方法,则可能不会调用 Application stop 方法。

    这个方法可以被任何线程调用。

    注意:如果应用程序嵌入在浏览器中,则此方法可能无效。


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

    if ("Quit".equals(menuLabel)) {
        // gameBoard is one of your @FXML annotated fields
        gameBoard.getScene().getWindow().hide();
    }
    

    这仅在gameBoard 所属的Window 是唯一打开的Window 时才有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多