javafx边框线_javafx分别设置四个边框

该篇博客介绍了如何在JavaFX中为组件设置不同边框,包括使用`setStyle`方法和`BorderStroke`类。通过代码示例展示了如何分别设置四个边框的颜色、样式和宽度,并提到了逗号与空格在边框颜色值中的区别。同时,博客指出官方文档中关于边框颜色值分隔的误导性信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package border;

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.layout.*;

import javafx.scene.paint.Color;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class BorderDemo extends Application {

@Override

public void start(Stage primaryStage) throws Exception {

Text text = new Text("test");

HBox root = new HBox();

root.getChildren().add(text);

root.setPadding(new Insets(50));

// root.setStyle("-fx-border-color: red orange black green;-fx-border-width: 50 40 20 10;-fx-border-style: solid dashed dotted dotted;");

// root.setStyle("-fx-border-width: 0 0 10 0;-fx-border-style: solid;-fx-border-color: red");

BorderStroke borderStroke = new BorderStroke(null,null, Color.BLACK,null, null,null,BorderStrokeStyle.SOLID,null,null, BorderWidths.DEFAULT,new Insets(50));

root.setBorder(new Border(borderStroke));

Scene scene = new Scene(root, 500, 300);

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

效果图

e8de62d29e9a48f1c3b04774dbfbed12.png

如果把style中的空格改为逗号,即root.setStyle("-fx-border-color: red,orange,black,green;...")

效果图

272217ea615fd22462b9d02e172b6c5c.png

看下官方文档,如果四个边框分开设置,就是要用逗号,实际却是用空格隔开

-fx-border-color

A series of paint values or sets of four paint values, separated by commas. For each item in the series, if a single paint value is specified, then that paint is used as the border for all sides of the region; and if a set of four paints is specified, they are used for the top, right, bottom, and left borders of the region, in that order. If the border is not rectangular, only the first paint value in the set is used.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值