如何在JavaFX舞台上创建自定义边框?

时间:2016-01-25 15:00:40

标签: java css javafx

我正在尝试在stage.initStyle(StageStyle.UNDECORATED);

上创建自定义边框

它不起作用,我不确定我做错了什么。

// Using Pane to able to customize children
Pane rootPane = new Pane(); 

// my main.css is src/main/resources
rootPane.getStylesheets().add("main.css"); 

// rootPane is the name of the css class
rootPane.getStyleClass().add("rootPane"); 

// creating the scene with the Pane layout.
Scene scene = new Scene(rootPane, model.getSceneWidth(), model.getSceneHeight()); 

// Fetch the file with classLoader and add css to the scene.
scene.getStylesheets().add(getClass().getClassLoader().getResource("main.css").toExternalForm());

stage.setScene(scene);       
stage.show();

我的main.css:

.rootPane {
  -fx-border-insets: 23;
  -fx-background-radius: 6;
  -fx-border-radius: 6;
  -fx-border-color: white;
  -fx-border-style: solid;
  -fx-border-width: 5;
  -fx-effect: dropshadow(three-pass-box, rgba(100, 100, 100, 1), 24, 0.5, 0, 0);
  -fx-background-insets: 23;
}

完整启动方法代码:

@Override
    public void start(final Stage stage) throws Exception {

            matrixStage = stage;
            matrixStage.setTitle("null");
            matrixStage.initStyle(StageStyle.UNDECORATED);

            Pane rootPane = new Pane();
            rootPane.getStylesheets().add("main.css");
            rootPane.getStyleClass().add("rootPane");

            Scene scene = new Scene(rootPane, matrixModel.getSceneWidth(), matrixModel.getSceneHeight());
            scene.getStylesheets().add(getClass().getClassLoader().getResource("main.css").toExternalForm());

            Canvas canvas = new Canvas();
            canvas.widthProperty().bind(matrixStage.widthProperty());
            canvas.heightProperty().bind(matrixStage.heightProperty());    

            rootPane.getChildren().add(canvas);
            matrixStage.setScene(scene);
            matrixStage.show();
    }

在我设置画布宽度和高度后,使用舞台属性绑定它,边框会消失。这是因为帆布覆盖了'自定义边框?它背后还是什么?

1 个答案:

答案 0 :(得分:1)

没错,它没有回答这个问题,但我已经达到了想要的效果。

我在画布上添加了一个样式:

canvas.setStyle("-fx-effect: innershadow(gaussian, #039ed3, 2, 1.0, 0, 0);"); 

将舞台设置为未修饰:

stage.initStyle(StageStyle.UNDECORATED);

然后在场景中创建按钮以最大/最小并关闭应用程序,这实际上最终成为更好的解决方案。