我创建了一个JavaFX项目,并为java Scene Builder中的第一个登录框架创建了GUI.登录成功后,必须关闭登录框,并且必须显示下一帧(主程序框).我可以使新框架出现,但我无法关闭登录框架.我尝试了类似dispose()但没什么用的东西.下面是主类的代码:
public class KuberComm extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setResizable(false);
stage.setTitle("Login to KuberComm");
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
登录按钮的处理程序位于另一个类(NetBeans IDE生成的控制器类)中.我无法弄清楚框架的名称是什么,以便在控制器类中使用它.
任何帮助都感激不尽!
Moh*_*-Aw 34
在控制器类中为您的按钮命名:
@FXML
public Button closeButton;
Run Code Online (Sandbox Code Playgroud)
并添加此方法:
@FXML
public void handleCloseButtonAction(ActionEvent event) {
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
}
Run Code Online (Sandbox Code Playgroud)
在FXML中,您需要引用按钮名称和调用onAction的方法:
<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" />
Run Code Online (Sandbox Code Playgroud)
这将关闭此按钮所在的阶段.
使用
stage.hide()
Run Code Online (Sandbox Code Playgroud)
如果从控制器执行此操作,则可以从舞台Node场景内的任何内部获取舞台(如果需要,让FXML加载程序使用idfxml中fxml命名空间中的属性将其分配给控制器的字段):
Window stage = node.getScene().getWindow();
Run Code Online (Sandbox Code Playgroud)
与其他答案类似但更精确.
@FXML
public void handleCloseButtonAction(ActionEvent event) {
((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}
Run Code Online (Sandbox Code Playgroud)
感谢您拨冗回覆,但最后我找到了解决方法。我用了
((Node)(event.getSource())).getScene().getWindow().hide();
Run Code Online (Sandbox Code Playgroud)
在if,它负责成功登录。我的意思是,在出现对话框通知用户成功登录之后,该代码就在那里了。
(为了使该行代码正常工作,我也导入了正确的内容)
| 归档时间: |
|
| 查看次数: |
57247 次 |
| 最近记录: |