JavaFX退出菜单项
如何通过单击菜单中的“退出”关闭程序? 我正在使用Scene Builder和FXML,所以我尝试在main类中创建一个方法closeOPT(),其中包含关闭我的应用程序的代码,但当我在controler中创建实例时,它不起作用。所以现在,我不知道如何让它工作。 我还尝试在主类中将close方法与FXML id连接起来,但这也不起作用JavaFX退出菜单项,java,javafx,Java,Javafx,如何通过单击菜单中的“退出”关闭程序? 我正在使用Scene Builder和FXML,所以我尝试在main类中创建一个方法closeOPT(),其中包含关闭我的应用程序的代码,但当我在controler中创建实例时,它不起作用。所以现在,我不知道如何让它工作。 我还尝试在主类中将close方法与FXML id连接起来,但这也不起作用 enter code here public class GameController { @FXML Button b1; @FXML Button
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);
}}
在此处输入代码
公共类游戏控制器{
@FXML按钮b1;
@FXML按钮b2;
@FXML按钮b3;
@FXML按钮b4;
@FXML按钮b5;
@FXML按钮b6;
@FXML按钮b7;
@FXML按钮b8;
@FXML按钮b9;
@FXML网格游戏板;
私有布尔值isFirstPlayer=true;
公共无效按钮ClickHandler(ActionEvent evt){
按钮clickedButton=(按钮)evt.getTarget();
String buttonLabel=clickedButton.getText();
如果(“.equals(按钮标签)&&isFirstPlayer){
单击按钮。设置文本(“X”);
isFirstPlayer=false;
}
如果(“.equals(buttonLabel)&&&!isFirstPlayer),则为else{
单击按钮。设置文本(“O”);
isFirstPlayer=true;
}
find3InARow();
}
私有布尔值find3InARow(){
//第1行
如果(“!=b1.getText()&&b1.getText()==b2.getText())
&&b2.getText()==b3.getText()){
highlightWinningCombo(b1、b2、b3);
返回true;
}
//第2排
如果(“!=b4.getText()&&b4.getText()==b5.getText())
&&b5.getText()==b6.getText()){
highlightWinningCombo(b4、b5、b6);
返回true;
}
//第3排
如果(“!=b7.getText()&&b7.getText()==b8.getText())
&&b8.getText()==b9.getText()){
highlightWinningCombo(b7、b8、b9);
返回true;
}
//第1栏
如果(“!=b1.getText()&&b1.getText()==b4.getText())
&&b4.getText()==b7.getText()){
highlightWinningCombo(b1、b4、b7);
返回true;
}
//第2栏
如果(“!=b2.getText()&&b2.getText()==b5.getText())
&&b5.getText()==b8.getText()){
highlightWinningCombo(b2、b5、b8);
返回true;
}
//第3栏
如果(“!=b3.getText()&&b3.getText()==b6.getText())
&&b6.getText()==b9.getText()){
highlightWinningCombo(b3、b6、b9);
返回true;
}
//对角线1
如果(“!=b1.getText()&&b1.getText()==b5.getText())
&&b5.getText()==b9.getText()){
highlightWinningCombo(b1、b5、b9);
返回true;
}
//对角线2
如果(“!=b3.getText()&&b3.getText()==b5.getText())
&&b5.getText()==b7.getText()){
highlightWinningCombo(b3、b5、b7);
返回true;
}
返回false;
}
private void highlightWinningCombo(按钮第一、按钮第二、按钮第三){
首先.getStyleClass().add(“获胜按钮”);
second.getStyleClass().add(“获胜按钮”);
第三,getStyleClass().add(“获胜按钮”);
}
公共无效菜单ClickHandler(ActionEvent evt){
MenuItem clickedMenu=(MenuItem)evt.getTarget();
String menuLabel=clickedMenu.getText();
如果(“播放”。等于(menuLabel)){
可观察列表按钮=
gameBoard.getChildren();
按钮。forEach(btn->{
((按钮)btn.setText(“”);
btn.getStyleClass().remove(“获胜按钮”);
});
isFirstPlayer=true;
}
如果(“退出”。等于(menuLabel)){
}
}
}
公共类主扩展应用程序{
初级阶段;
@凌驾
公共无效开始(阶段primaryStage){
试一试{
BorderPane root=(BorderPane)FXMLLoader.load(getClass().getResource(“KrizicKruzigIgra.fxml”);
场景=新场景(根,300320);
scene.getStylesheets().add(getClass().getResource(“application.css”).toExternalForm());
初级阶段。场景(场景);
primaryStage.SetResizeable(假);
primaryStage.show();
}捕获(例外e){
e、 printStackTrace();
}
}
公共无效关闭选项(阶段primaryStage){
primaryStage.close();
}
公共静态void main(字符串[]args){
发射(args);
}}
如果您希望能够从任何地方终止JavaFX应用程序,请使用
导致JavaFX应用程序终止。如果在调用应用程序启动方法后调用此方法,则JavaFX启动器将调用应用程序停止方法并终止JavaFX应用程序线程。然后启动器线程将关闭。如果没有其他正在运行的非守护进程线程,Java VM将退出。如果从预加载程序或应用程序初始化方法调用此方法,则可能不会调用应用程序停止方法
可以从任何线程调用此方法
注意:如果应用程序嵌入到浏览器中,则此方法可能无效
另一种方法是关闭所有打开的窗口,只要返回
true
(请参阅);看起来这可能就是你一开始想要做的
if ("Quit".equals(menuLabel)) {
// gameBoard is one of your @FXML annotated fields
gameBoard.getScene().getWindow().hide();
}
只有当游戏板
所属的窗口
是唯一打开的窗口
时,此选项才有效