Administrator
Published on 2024-04-15 / 22 Visits
0
0

javafx实现圆角窗口

实现圆形窗口

  1. screen和sagte配置成透明样式,代码参考:
public class Application extends javafx.application.Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Application.class.getResource("main.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        scene.setFill(Color.TRANSPARENT); //透明样式
        stage.initStyle(StageStyle.TRANSPARENT); //透明样式
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
  1. fxml文件的根容器配置sytyle属性css样式如下:
style="-fx-background-color: #E6F3FD; -fx-border-radius: 20; -fx-border-color: black; -fx-border-width: 2px; -fx-background-radius: 20; "

其中-fx-background-radius是为了背景呈现圆角,如果不加则会:
1713193277630.png
其中-fx-border-radius是为了边框呈现圆角,如果不加则会:
1713194002710.png
在使用边框的情况下,-fx-background-radius和-fx-border-radius的取值需要一致。
如果-fx-border-radius大的话,则会:
1713193619552.png
如果-fx-background-radius大的话,则会:
1713193792446.png


Comment