JavaFX Button自适应窗口(跟着窗口大小变化)

在实际的应用中,经常会需要Button或者其他的控件跟随窗口的变化而变化,下面给出Demo。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * Created by wzj on 2018/3/29.
 */
public class AutoSizeButton extends Application
{
    /**
     * The main entry point for all JavaFX applications.
     * The start method is called after the init method has returned,
     * and after the system is ready for the application to begin running.
     * <p>
     * <p>
     * NOTE: This method is called on the JavaFX Application Thread.
     * </p>
     *
     * @param primaryStage the primary stage for this application, onto which
     *                     the application scene can be set. The primary stage will be embedded in
     *                     the browser if the application was launched as an applet.
     *                     Applications may create other stages, if needed, but they will not be
     *                     primary stages and will not be embedded in the browser.
     */
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Button button1 = new Button("Button1");
        Button button2 = new Button("Button2");

        button1.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
        button2.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);

        Rectangle rect1 = new Rectangle(60,20);
        rect1.setFill(Color.TRANSPARENT);

        Rectangle rect2 = new Rectangle(60,20);
        rect2.setFill(Color.TRANSPARENT);

        HBox hBox = new HBox(10,rect1,button1,button2,rect2);
        hBox.setAlignment(Pos.CENTER);

        HBox.setHgrow(button1, Priority.ALWAYS);
        HBox.setHgrow(button2, Priority.ALWAYS);

        BorderPane root = new BorderPane();
        root.setBottom(hBox);

        Scene scene = new Scene(root,300,250);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

实际效果如下:


  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

dmfrm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值