JavaFX 自定义CSS样式

 

/* (图1) 设置背景颜色渐变,where <side-or-corner> = [left | right] || [top | bottom] */
-fx-background-color: linear-gradient(to bottom,#99CCFF,#CCFFFF)
图1

 

/* (图2) 边角圆弧,参数分别是左上角→右上角→左下角→右下角,全部统一就只输50 */
-fx-background-radius: 50 0 50 0
图2

 

/* (图3) 内边距 */
-fx-background-insets:10 0 10 0
图3

 

 

/* (图4) 标题面板  */
-fx-background-color: linear-gradient(from 0px 0px to 0px 50px, gray, darkgray 50%, dimgray 99%, white)
-fx-background-radius: 30 30 0 0
图4

 


设置TextArea文本域透明(背景透明,文字不透明)


待续...

 

 

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX提供了一种简单的方法来自定义窗口样式,可以使用CSS样式表来改变窗口的外观。以下是步骤: 1. 创建一个新的CSS文件,例如“custom.css”。 2. 在CSS文件中定义你想要的样式,例如: ```css .root { -fx-background-color: #333; } .title-bar { -fx-background-color: #444; -fx-text-fill: white; } .close-button { -fx-background-color: #f00; -fx-text-fill: white; } ``` 3. 在JavaFX应用程序的启动方法中加载CSS文件,并将样式应用到窗口。 ```java public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Custom Window Style"); // Load custom CSS style Scene scene = new Scene(root); scene.getStylesheets().add(getClass().getResource("custom.css").toExternalForm()); // Apply custom style to window primaryStage.initStyle(StageStyle.UNDECORATED); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } ``` 4. 在FXML文件中添加一个带有“title-bar”类的Pane,以模拟窗口标题栏。 ```xml <Pane id="title-bar" styleClass="title-bar" onMousePressed="#handleMousePressed" onMouseDragged="#handleMouseDragged"> <Label text="Custom Window Style" /> <Button id="close-button" styleClass="close-button" text="X" onMouseClicked="#handleCloseClicked" /> </Pane> ``` 5. 在控制器类中添加处理鼠标事件的方法,使窗口可以拖动和关闭。 ```java public class Controller { @FXML private void handleMousePressed(MouseEvent event) { xOffset = event.getSceneX(); yOffset = event.getSceneY(); } @FXML private void handleMouseDragged(MouseEvent event) { Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); } @FXML private void handleCloseClicked(MouseEvent event) { Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); stage.close(); } } ``` 这样,你就可以使用CSS样式表来创建自定义窗口样式了。注意,需要自己添加拖动和关闭窗口的代码逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值