JavaFx:按钮边框和悬停

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30680570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:58:49  来源:igfitidea点击:

JavaFx: Button border and hover

javacssjavafxjava-8

提问by

I am using Java 8. I have toolbar and buttons on it.

我使用的是 Java 8。我有工具栏和按钮。

I want to implement the following:

我想实现以下内容:

  • In usual state (without mouse hover) at toolbar, only button label must be seen (no background, nor borders).
  • When the user mouse hovers over the button, then the usual button must be seen.
  • 在工具栏的通常状态下(没有鼠标悬停),只能看到按钮标签(没有背景,也没有边框)。
  • 当用户鼠标悬停在按钮上时,必须看到通常的按钮。

How to do it via css?

如何通过css做到这一点?

采纳答案by ItachiUchiha

Use the styles to remove the background :

使用样式删除背景:

.button {
    -fx-background-color: transparent;
}

On hover, to bring back everything just use the button style from modena.css:

在悬停时,只需使用以下按钮样式即可恢复所有内容modena.css

.button:hover{
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
    -fx-background-insets: 0 0 -1 0, 0, 1, 2;
    -fx-background-radius: 3px, 3px, 2px, 1px;
    -fx-padding: 0.333333em 0.666667em 0.333333em 0.666667em; /* 4 8 4 8 */
    -fx-text-fill: -fx-text-base-color;
    -fx-alignment: CENTER;
    -fx-content-display: LEFT;
}

回答by Rahel Lüthy

Background transparency can be controlled through mouse listeners, i.e. by setting the CSS programmatically (which alleviates the need for an external stylesheet):

可以通过鼠标侦听器控制背景透明度,即通过以编程方式设置 CSS(这减少了对外部样式表的需要):

final String IDLE_BUTTON_STYLE = "-fx-background-color: transparent;";
final String HOVERED_BUTTON_STYLE = "-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;";


button.setStyle(IDLE_BUTTON_STYLE);
button.setOnMouseEntered(e -> button.setStyle(HOVERED_BUTTON_STYLE));
button.setOnMouseExited(e -> button.setStyle(IDLE_BUTTON_STYLE));

回答by mohammed thaha jk

I agree with netzwerg. If you are using fxml with javafx then you should include these lines of codes in initialize() method of your controller class.

我同意 netzwerg。如果您将 fxml 与 javafx 一起使用,那么您应该在控制器类的 initialize() 方法中包含这些代码行。

Example of controller class:

控制器类示例:

public class Controller implements Initializable{
    private Button test;
    private static final String IDLE_BUTTON_STYLE = "-fx-background-color: transparent;";
    private static final String HOVERED_BUTTON_STYLE = "-fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;";

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        button.setStyle(IDLE_BUTTON_STYLE);
        button.setOnMouseEntered(e -> button.setStyle(HOVERED_BUTTON_STYLE));
        button.setOnMouseExited(e -> button.setStyle(IDLE_BUTTON_STYLE));
    }
}