java如何使窗体关闭按钮弹窗_如何通过单击按钮关闭Java窗口 – JavaFX项目

我创建了一个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);

}

}

登录按钮的处理程序位于另一个类(NetBeans IDE生成的控制器类)中.我无法弄清楚框架的名称是什么,以便在控制器类中使用它.

任何帮助都感激不尽!

解决方法:

在控制器类中为您的按钮命名:

@FXML

public Button closeButton;

并添加此方法:

@FXML

public void handleCloseButtonAction(ActionEvent event) {

Stage stage = (Stage) closeButton.getScene().getWindow();

stage.close();

}

在FXML中,您需要引用按钮名称和调用onAction的方法:

这将关闭此按钮所在的阶段.

标签:java,javafx

来源: https://codeday.me/bug/20190926/1819017.html

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX中,您可以通过使用ImageView和Button类来创建一个带有图像的自定义按钮。以下是一个简单的示例代码,它将为您演示如何创建一个具有图像的自定义按钮。 ```java import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class CustomButtonWithImage extends Application { @Override public void start(Stage primaryStage) { // Create an ImageView object to hold the image Image image = new Image("https://www.example.com/button-image.png"); ImageView imageView = new ImageView(image); // Create a Button object with the ImageView as its graphic Button button = new Button("", imageView); // Set the size of the button button.setPrefSize(100, 50); // Create a StackPane to hold the button StackPane root = new StackPane(); root.getChildren().add(button); root.setAlignment(Pos.CENTER); // Create a Scene with the StackPane as its root node Scene scene = new Scene(root, 300, 250); // Set the title of the Stage and add the Scene to it primaryStage.setTitle("Custom Button with Image"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 在此示例中,我们首先创建了一个ImageView对象来容纳我们要用作按钮图像的图像。然后,我们创建了一个Button对象并将该ImageView对象设置为其图形。我们还为按钮设置了一个首选大小,并将其放置在StackPane中,并将该StackPane设置为场景的根节点。最后,我们将场景添加到舞台中并显示它。 您可以通过替换代码中的图像URL来更改按钮的图像。您还可以更改按钮的大小和其他属性以满足您的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值