• JavaFX:控件边框设置


    JavaFX中控件的边框也可以进行设置。主要有两种方式,一种是Java方式,一种是CSS方式。

    JavaFX中控件继承自Region类。setBorder方法用来设置边框属性

    1. // The border of the Region, which is made up of zero or more BorderStrokes, and zero
    2. // or more BorderImages. It is possible for a Border to be empty, where it has neither
    3. // strokes nor images, and is semantically equivalent to null.
    4. public final void setBorder(Border value)

    边框类Border是一个不可变的对象,它封装了渲染区域边框所需的全部数据集。由于该类是不可变的,因此可以在多个不同的 "区域 "中自由重复使用相同的边框。

    1. class Border {
    2. // 四个构造函数。
    3. public Border(List strokes, List images);
    4. public Border(BorderStroke[] strokes, BorderImage[] images);
    5. public Border(BorderStroke... strokes);
    6. public Border(BorderImage... images);
    7. }

    每个边框类Border都由笔画BorderStoke和/或图像组成。两个列表都不会为空,但其中一个或两个都可能为空。在渲染时,如果没有指定图像或没有图像加载成功,那么所有笔画都将按顺序渲染。如果指定了任何图像且加载成功,则不会绘制任何笔画,但这些笔画仍会对边框的内侧和外侧产生影响。

    1. class BorderStroke {
    2. public BorderStroke(Paint stroke,
    3. BorderStrokeStyle style,
    4. CornerRadii radii,
    5. BorderWidths widths);
    6. public BorderStroke(Paint stroke,
    7. BorderStrokeStyle style,
    8. CornerRadii radii,
    9. BorderWidths widths,
    10. Insets insets);
    11. public BorderStroke(Paint topStroke,
    12. Paint rightStroke,
    13. Paint bottomStroke,
    14. Paint leftStroke,
    15. BorderStrokeStyle topStyle,
    16. BorderStrokeStyle rightStyle,
    17. BorderStrokeStyle bottomStyle,
    18. BorderStrokeStyle leftStyle,
    19. CornerRadii radii,
    20. BorderWidths widths,
    21. Insets insets);
    22. }
    23. topStroke - The fill to use on the top. If null, defaults to Color.BLACK.
    24. rightStroke - The fill to use on the right. If null, defaults to the same value as topStroke.
    25. bottomStroke - The fill to use on the bottom. If null, defaults to the same value as topStroke.
    26. leftStroke - The fill to use on the left. If null, defaults to the same value as rightStroke.
    27. topStyle - The style to use on the top. If null, defaults to BorderStrokeStyle.NONE.
    28. rightStyle - The style to use on the right. If null, defaults to the same value as topStyle.
    29. bottomStyle - The style to use on the bottom. If null, defaults to the same value as topStyle.
    30. leftStyle - The style to use on the left. If null, defaults to the same value as rightStyle.
    31. radii - The radii. If null, defaults to square corners by using CornerRadii.EMPTY.
    32. widths - The thickness of each side. If null, defaults to DEFAULT_WIDTHS.
    33. insets - The insets indicating where to draw the border relative to the region edges. If null, defaults to Insets.EMPTY.

    定义边框的描边类BorderStroke,用于为区域设计样式。描边是一种基于矢量的渲染,用于勾勒边框区域的轮廓。它可以从 Region 的边缘嵌入(或外移),在计算 Region 的嵌入(用于定义内容区域)时,会考虑到描边的值。在使用任何边框图像时,都不会使用描边视觉效果。

    BorderStrokeStyle:定义 BorderStroke 一侧使用的描边样式。有几种预定义的样式,不过这些预定义样式的属性可能与最终绘制它们时使用的设置不一致。您也可以创建一个新的 BorderStrokeStyle,然后手动定义每个描边设置,这与任何形状类似。注:BorderStrokeStyle.NONE是默认值,设置边框不显示。

    具体示例:

    1. package javafx8.ch10;
    2. import javafx.application.Application;
    3. import javafx.geometry.Rectangle2D;
    4. import javafx.scene.Scene;
    5. import javafx.scene.layout.Border;
    6. import javafx.scene.layout.BorderStroke;
    7. import javafx.scene.layout.BorderStrokeStyle;
    8. import javafx.scene.layout.BorderWidths;
    9. import javafx.scene.layout.CornerRadii;
    10. import javafx.scene.layout.Pane;
    11. import javafx.scene.paint.Paint;
    12. import javafx.stage.Screen;
    13. import javafx.stage.Stage;
    14. /**
    15. * @copyright 2003-2023
    16. * @package javafx8.ch10
    17. * @file BorderTest.java
    18. * @date 2023-10-16 15:47
    19. * @author qiao wei
    20. * @version 1.0
    21. * @brief 测试控件边框设置。
    22. * @history
    23. */
    24. public class BorderTest extends Application {
    25. public BorderTest() {}
    26. @Override
    27. public void start(Stage primaryStage) throws Exception {
    28. start02(primaryStage).show();
    29. }
    30. public static void main(String[] args) {
    31. Application.launch(BorderTest.class, args);
    32. }
    33. /**
    34. * @class BorderTest
    35. * @date 2023-10-16 16:13
    36. * @author qiao wei
    37. * @version 1.0
    38. * @brief 创建Stage。边框设置不做任何设置。
    39. * @param primaryStage 主窗体。
    40. * @return 修改后的主窗体primaryStage。
    41. * @throws
    42. */
    43. private Stage start01(Stage primaryStage) {
    44. Pane pane = new Pane();
    45. // 设置pane尺寸。
    46. Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
    47. double screenWidth = screenRectangle.getWidth();
    48. double screenHeight = screenRectangle.getHeight();
    49. pane.setPrefSize(screenWidth / 3, screenHeight / 3);
    50. primaryStage.setScene(new Scene(pane));
    51. primaryStage.setTitle("添加Pane,对边框不做任何设置");
    52. return primaryStage;
    53. }
    54. /**
    55. * @class BorderTest
    56. * @date 2023-10-16 17:37
    57. * @author qiao wei
    58. * @version 1.0
    59. * @brief
    60. * @param
    61. * @return
    62. * @throws
    63. */
    64. private Stage start02(Stage primaryStage) {
    65. Pane pane = new Pane();
    66. // 设置pane尺寸。
    67. Rectangle2D screenRectangle = Screen.getPrimary().getBounds();
    68. double screenWidth = screenRectangle.getWidth();
    69. double screenHeight = screenRectangle.getHeight();
    70. pane.setPrefSize(screenWidth / 3, screenHeight / 3);
    71. // 设置边框画笔。BorderStrokeStyle字段设置绘制边框,NONE不显示边框,DASHED、DOTTED、SOLD是预定义的边框。
    72. BorderStroke borderStroke = new BorderStroke(
    73. Paint.valueOf("#00FF00"),
    74. BorderStrokeStyle.DOTTED,
    75. new CornerRadii(30),
    76. new BorderWidths(20)
    77. );
    78. pane.setBorder(new Border(borderStroke));
    79. primaryStage.setScene(new Scene(pane));
    80. primaryStage.setTitle("添加Pane,设置BorderStroke");
    81. return primaryStage;
    82. }
    83. }

    start01方法结果:

    start02方法结果:

  • 相关阅读:
    网络安全学习路线推荐
    Ajax及跨域请求
    2022/8/11
    Mac 下 Python+Selenium 自动上传西瓜视频
    VSCode自动分析代码的插件
    Vue.js基础语法下
    MySQL——后码锁(Next-Key Block)
    记一次 处理MIUI 13 疯狂杀进程问题
    如何优雅的使用 IDEA Debug 进行调试
    CSS 之 grid 网格布局
  • 原文地址:https://blog.csdn.net/weiweiqiao/article/details/133847156