JavaFX的窗口监听

编写窗口监听类

package sample;

import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import java.util.Optional;

public class ListennerWindows {
   
    /**
     * 监听窗口类
     * @param primaryStage
     */
    public static void Listenner(Stage primaryStage){
   
        primaryStage.setOnCloseRequest<
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现JavaFX窗口自适应,需要在布局中使用相对大小而不是固定大小,可以使用JavaFX的AnchorPane布局或GridPane布局来实现。 在AnchorPane布局中,可以使用AnchorPane.setTopAnchor、AnchorPane.setBottomAnchor、AnchorPane.setLeftAnchor和AnchorPane.setRightAnchor方法来设置组件的位置和大小。 在GridPane布局中,可以使用ColumnConstraints和RowConstraints来设置列和行的大小,然后使用GridPane.setHgrow和GridPane.setVgrow方法来设置组件的大小和位置。 另外,可以使用Scene.widthProperty和Scene.heightProperty来监听窗口的大小变化,并在变化时更新布局。 下面是一个简单的例子,展示如何使用AnchorPane布局实现JavaFX窗口自适应: ```java public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { AnchorPane root = new AnchorPane(); Button button = new Button("Hello World"); root.getChildren().add(button); AnchorPane.setTopAnchor(button, 50.0); AnchorPane.setLeftAnchor(button, 50.0); AnchorPane.setRightAnchor(button, 50.0); AnchorPane.setBottomAnchor(button, 50.0); Scene scene = new Scene(root, 400, 300); primaryStage.setScene(scene); primaryStage.show(); scene.widthProperty().addListener((obs, oldVal, newVal) -> { AnchorPane.setLeftAnchor(button, newVal.doubleValue() / 4); AnchorPane.setRightAnchor(button, newVal.doubleValue() / 4); }); scene.heightProperty().addListener((obs, oldVal, newVal) -> { AnchorPane.setTopAnchor(button, newVal.doubleValue() / 4); AnchorPane.setBottomAnchor(button, newVal.doubleValue() / 4); }); } } ``` 在这个例子中,按钮的位置和大小使用AnchorPane.setTopAnchor、AnchorPane.setBottomAnchor、AnchorPane.setLeftAnchor和AnchorPane.setRightAnchor方法来设置。在窗口大小变化时,使用Scene.widthProperty和Scene.heightProperty监听窗口的大小变化,并更新按钮的位置和大小,使其保持在窗口中间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值