代码之家  ›  专栏  ›  技术社区  ›  Roman Kurbanov

如何在JavaFX中设置按钮内的文本以放置阴影

  •  1
  • Roman Kurbanov  · 技术社区  · 10 年前

    所以我在控制器中有一个自定义的按钮类,我想制作一个字体,它将位于按钮的内部,以放置阴影。我尝试过使用-fx笔划,但似乎没有任何改变。我想使用的按钮将在程序期间生成。我不熟悉CSS,所以我只使用了一些示例。现在我有了这个

    mineButton(int x, int y,boolean difficult){ 
            this.x=x;
            this.y=y;
            Font s = new Font(13);
            if (difficult){
                this.setMaxSize(35, 35);
                this.setMinSize(20, 20);
                this.setPrefSize(20, 20);
                this.setFont(new Font(8));
            } else {
                this.setMaxSize(35,35);
                this.setMinSize(33,33);
                this.setPrefSize(34,34);
            }
            this.setStyle("-fx-background-color: -fx-outer-border,       
            -fx-inner-border, -fx-body-color;\n" +
                    "    -fx-background-insets: 0, 1, 2;" +
                    "    -fx-background-radius: 5, 4, 3;" + 
                    " -fx-text-fill: white; -fx-font-weight: bold;" ); 
        }
    
    1 回复  |  直到 10 年前
        1
  •  3
  •   Roland    10 年前

    您可以使用 setGraphic 方法更改按钮内节点的外观。

    下面是一个文档,其中包含一个如何执行的示例: Using JavaFX UI Controls - Button .

    然后,您可以将CSS应用于您的自定义节点。

    例子:

    Button button = new Button();
    Label label = new Label("Click Me!");
    label.setStyle("-fx-effect: dropshadow( one-pass-box , black , 8 , 0.0 , 2 , 0 )");
    button.setGraphic(label);
    
    推荐文章