JavaFX:Insets在控件中使用简例

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.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.DOTTED,
                    null,
                    new BorderWidths(20),
                    new Insets(5)
                )
            )
        );
//        button.setBackground(
//            new Background(
//                new BackgroundFill(
//                    Paint.valueOf("#8FBC8F"),
//                    null,
//                    Insets.EMPTY
//                )
//            )
//        );
        
        HBox hBox = new HBox();
        HBox.setMargin(button, new Insets(5, 200, 100, 200));
        hBox.getChildren().add(button);
        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();
        }
    }
}

测试结果如下:

JavaFX:Insets在控件中使用简例_第1张图片

你可能感兴趣的:(JavaFX,Java,JavaFX,java)