JavaFX:Insets在控件中使用简例

104 篇文章 5 订阅

Insets是矩形区域 4 边的一组内偏移量,矩形内的设置与边框距离。如果在周围的边框有控件,则是矩形与控件的距离。

package javafx8.ch10;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

/**
 * @copyright 2023-2022
 * @package   javafx8.ch10
 * @file      InsetsTest.java
 * @date      2023-08-25 12:46
 * @author    qiao wei
 * @version   1.0
 * @brief     
 * @history
 */
public class InsetsTest extends Application {
    
    public InsetsTest() {}    

    @Override
    public void start(Stage primaryStage) throws Exception {
        /**
         * 
         */
        Button button = new Button("Insets Test");
        button.setBorder(
            new Border(
                new BorderStroke(
                    null,
                    BorderStrokeStyle.SOLID,
                    null,
                    new BorderWidths(1),
                    new Insets(2)
                )
            )
        );
        
        Button button01 = new Button("Insets02");
        button01.setBorder(
            new Border(
                new BorderStroke(
                    null,
                    BorderStrokeStyle.DASHED,
                    new CornerRadii(10),
                    new BorderWidths(2),
                    new Insets(5)
                )
            )
        );
        
//        button.setBackground(
//            new Background(
//                new BackgroundFill(
//                    Paint.valueOf("#8FBC8F"),
//                    null,
//                    Insets.EMPTY
//                )
//            )
//        );
        
        HBox hBox = new HBox();
        
        // 设置控件button在hBox中与边距的设置。因为button右侧有控件button1,则右侧边距为与控件button1的距离。
        HBox.setMargin(button, new Insets(5, 10, 150, 10));
        hBox.getChildren().addAll(button, button01);
        hBox.setBackground(
            new Background(
                new BackgroundFill(Paint.valueOf("#54FF9F"),
                    null,
                    null
                )
            )
        );
        Scene scene = new Scene(hBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        try {
            Application.launch(InsetsTest.class, args);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

测试结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值