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();
}
}
}
测试结果如下: