alf*_*923 48 java user-interface javafx fxml
我需要通过控制器中的代码关闭当前的fxml窗口
我知道stage.close()或stage.hide()在fx中执行此操作
如何在fxml中实现这个?我试过了
private void on_btnClose_clicked(ActionEvent actionEvent) {
Parent root = FXMLLoader.load(getClass().getResource("currentWindow.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
所有帮助将不胜感激.谢谢!
a.b*_*a.b 135
<Button fx:id="closeButton" onAction="#closeButtonAction">
在您的控制器类中:
@FXML private javafx.scene.control.Button closeButton;
@FXML
private void closeButtonAction(){
// get a handle to the stage
Stage stage = (Stage) closeButton.getScene().getWindow();
// do what you have to do
stage.close();
}
Run Code Online (Sandbox Code Playgroud)cod*_*leb 19
如果您有一个扩展的窗口,javafx.application.Application;
您可以使用以下方法.(这将关闭整个应用程序,而不仅仅是窗口.我误解了OP,感谢评论者指出它).
Platform.exit();
Run Code Online (Sandbox Code Playgroud)
例:
public class MainGUI extends Application {
.........
Button exitButton = new Button("Exit");
exitButton.setOnAction(new ExitButtonListener());
.........
public class ExitButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent arg0) {
Platform.exit();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑Java 8之美:
public class MainGUI extends Application {
.........
Button exitButton = new Button("Exit");
exitButton.setOnAction(actionEvent -> Platform.exit());
}
Run Code Online (Sandbox Code Playgroud)
我NullPointerException
从接受的答案中收到一个后,按照以下方式实现了这一点.
在我的FXML中:
<Button onMouseClicked="#onMouseClickedCancelBtn" text="Cancel">
Run Code Online (Sandbox Code Playgroud)
在我Controller
班上:
@FXML public void onMouseClickedCancelBtn(InputEvent e) {
final Node source = (Node) e.getSource();
final Stage stage = (Stage) source.getScene().getWindow();
stage.close();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
140746 次 |
最近记录: |