【问题标题】:JavaFX TitledPane changed title background is reset on mouse enteredJavaFX TitledPane 更改的标题背景在鼠标输入时重置
【发布时间】:2018-10-31 12:08:56
【问题描述】:

我需要在运行时根据一些传入的值更改 TitledPane 标题背景,否则将其重置。

我所有的 TitledPane 的样式都基于附加到场景的 CSS。

改变背景没问题。 问题是当背景改变后鼠标进入标题时,背景会被CSS重置为背景。


更改 TitledPane 标题背景的测试应用:

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TitledPaneApplication extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final StackPane root = new StackPane();

        final TitledPane titledPane = new TitledPane();
        titledPane.setText("Title");
        root.getChildren().add(titledPane);

        final String titleBackgroundValue = "#00ff11";
        final ToggleButton button = new ToggleButton("Change");
        button.setOnAction(event -> {
            boolean selected = button.isSelected();

            final Node node = titledPane.lookup(".title");
            if (selected) {
                final Color color = Color.valueOf(titleBackgroundValue);
                ((Region) node).setBackground(new Background(new BackgroundFill(color, null, null)));
            } else {
                ((Region) node).setBackground(null);
                titledPane.applyCss();
            }
        });

        button.setSelected(false);
        titledPane.setContent(button);

        final Scene scene = new Scene(root, 400, 400);
        scene.getStylesheets().add(getClass().getResource("light.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("TestApplication");
        primaryStage.show();
    }

}

场景 CSS light.css

.root {
    -fx-base: rgb(240, 240, 240);
    -fx-background: rgb(240, 240, 240);
    -fx-border-color: rgb(220, 220, 220);

    /* make controls (buttons, thumb, etc.) slightly lighter */
    -fx-color: derive(-fx-base, 10%);

    /* text fields and table rows background */
    -fx-control-inner-background: rgb(248, 248, 248);
    /* version of -fx-control-inner-background for alternative rows */
    -fx-control-inner-background-alt: derive(-fx-control-inner-background, -2.5%);

    /* text colors depending on background's brightness */
    -fx-light-text-color: rgb(220, 220, 220);
    -fx-mid-text-color: rgb(100, 100, 100);
    -fx-dark-text-color: rgb(20, 20, 20);

    /* A bright blue for highlighting/accenting objects.  For example: selected
     * text; selected items in menus, lists, trees, and tables; progress bars */
    -fx-accent: rgb(0, 80, 100);

    /* color of non-focused yet selected elements */
    -fx-selection-bar-non-focused: rgb(50, 50, 50);

    -fx-font-family: "Roboto"; /* "Segoe UI Semibold", "Roboto", "Monospaced" */
    -fx-font-size: 1em;

    -primary-border-color: rgb(220, 220, 220);
}

/* Fix derived prompt color for text fields */
.text-input {
    -fx-prompt-text-fill: derive(-fx-control-inner-background, -50%);
}

/* Keep prompt invisible when focused (above color fix overrides it) */
.text-input:focused {
    -fx-prompt-text-fill: transparent;
}

/* Fix scroll bar buttons arrows colors */
.scroll-bar > .increment-button > .increment-arrow,
.scroll-bar > .decrement-button > .decrement-arrow {
    -fx-background-color: -fx-mark-highlight-color, rgb(220, 220, 220);
}

.scroll-bar > .increment-button:hover > .increment-arrow,
.scroll-bar > .decrement-button:hover > .decrement-arrow {
    -fx-background-color: -fx-mark-highlight-color, rgb(240, 240, 240);
}

.scroll-bar > .increment-button:pressed > .increment-arrow,
.scroll-bar > .decrement-button:pressed > .decrement-arrow {
    -fx-background-color: -fx-mark-highlight-color, rgb(255, 255, 255);
}

.text-field {
    -fx-font-size: 10pt;
}

.combo-box {
    -fx-font-size: 10pt;
}

/* ScrollPane style. */
.scroll-pane {
    -fx-background-color: transparent;
}

.scroll-pane > .viewport {
    -fx-background-color: transparent;
}

/* TabPane style. */
.tab-pane > .tab-header-area {
    -fx-background-color: transparent;
}

/* TitledPane style. */
.titled-pane {
    -fx-border-width: 1;
    -fx-border-color: -primary-border-color;
    -fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.15), 5, 0.0, 0, 1);
}

.titled-pane > .content {
    -fx-border-width: 0;
}

.titled-pane .title .arrow-button {
    visibility: false;
}

.titled-pane > .title {
    -fx-background-color: -primary-border-color;
    -fx-background-insets: 0;
    -fx-background-radius: 0 0 0 0;
    -fx-padding: 0.2em 0.2em 0.2em 0.2em;
}

.titled-pane > .title .text {
    -fx-font-size: 10pt;
}

【问题讨论】:

  • -primary-border-color 定义在哪里?
  • 很抱歉。我已将它添加到 CSS 示例中。
  • 很奇怪。我尝试了许多纠正此问题的方法,但无法隔离悬停时触发的属性...

标签: java css javafx


【解决方案1】:

设置内联 CSS 样式优先于 css 文件中的样式。所以通过 setStyle 应用背景就可以了。

button.setOnAction(event -> {
            final Node node = titledPane.lookup(".title");
            if (button.isSelected()) {
                node.setStyle("-fx-background-color:#00ff11;");
            } else {
                node.setStyle(null);
            }
        });

更新: 但是,关于实际问题的更多细节。要理解这一点,首先需要了解 .title 的背景是如何在内部定义的,以及悬停样式是如何设置的。

Modena.css 内部是 .title 的背景样式:

.titled-pane > .title {
    -fx-background-color:
        linear-gradient(to bottom,
            derive(-fx-color,-15%) 95%,
            derive(-fx-color,-25%) 100%
        ),
        -fx-inner-border, -fx-body-color;
    -fx-background-insets: 0, 1, 2;
    -fx-background-radius: 3 3 0 0, 2 2 0 0, 1 1 0 0;
    -fx-padding: 0.3333em 0.75em 0.3333em 0.75em; /* 4 9 4 9 */
}

.titled-pane > .title:hover {
    -fx-color: -fx-hover-base;
}

如果您注意到实际背景来自 -fx-color、-fx-inner-border 和 -fx-body-color。但是 -fx-inner-border 和 -fx-body-color 确实又是从 -fx-color 派生的。

-fx-inner-border: linear-gradient(to bottom,
                ladder(
                    -fx-color,
                    derive(-fx-color,30%) 0%,
                    derive(-fx-color,20%) 40%,
                    derive(-fx-color,25%) 60%,
                    derive(-fx-color,55%) 80%,
                    derive(-fx-color,55%) 90%,
                    derive(-fx-color,75%) 100%
                ),
                ladder(
                    -fx-color,
                    derive(-fx-color,20%) 0%,
                    derive(-fx-color,10%) 20%,
                    derive(-fx-color,5%) 40%,
                    derive(-fx-color,-2%) 60%,
                    derive(-fx-color,-5%) 100%
                ));
                
-fx-body-color: linear-gradient(to bottom,
            ladder(
                -fx-color,
                derive(-fx-color,8%) 75%,
                derive(-fx-color,10%) 80%
            ),
            derive(-fx-color,-8%));
    

在 :hover 伪状态下,-fx-color 更改为 -fx-hover-base 并相应更新背景。 这就是你的问题。您仅以编程方式设置默认背景。在将鼠标悬停在 .title 上时,它仍会选择内部 CSS 文件样式(因为您尚未定义自定义的悬停样式)。

如果我们设法更新了 -fx-color 属性,那么它将负责不同伪状态的相应 css 更新。

更正确的方法满足您的要求:这样您仍然可以获得内部定义的标题的漂亮渐变功能。

button.setOnAction(event -> {
            final Node node = titledPane.lookup(".title");
            if (button.isSelected()) {
                node.setStyle("-fx-color:#00ff11;");
            } else {
                node.setStyle(null);
            }
        });
    
// In css file  
.titled-pane > .title {
    -fx-color: -primary-border-color;
    -fx-background-insets: 0;
    -fx-background-radius: 0 0 0 0;
    -fx-padding: 0.2em 0.2em 0.2em 0.2em;
}

更新 2

请在下面找到另一个 TitledPane 中的 TitledPane 示例。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Set;

public class TitledPaneApplication extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final VBox root = new VBox();
        root.setSpacing(10);

        final TitledPane titledPane = new TitledPane();
        titledPane.setText("Title");

        final String titleBackgroundValue = "#00ff11";
        final ToggleButton button = new ToggleButton("Change");
        button.setOnAction(event -> {
            final Set<Node> node = titledPane.lookupAll(".title");
            if (button.isSelected()) {
                node.forEach(n->n.setStyle("-fx-color:#00ff11;"));
            } else {
                node.forEach(n->n.setStyle(null));
            }
        });
        button.setSelected(false);

        VBox inner = new VBox();
        inner.setSpacing(10);
        inner.setPadding(new Insets(10));
        final TitledPane innerTP = new TitledPane();
        innerTP.setText("Inner Title");
        inner.getChildren().addAll(new Label("Inner"),innerTP);
        innerTP.setContent(new Button("Dummy Button"));

        titledPane.setContent(inner);

        root.getChildren().addAll(button,titledPane);
        final Scene scene = new Scene(root, 400, 400);
        scene.getStylesheets().add(getClass().getResource("light.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("TestApplication");
        primaryStage.show();
    }
}

【讨论】:

  • 这似乎可以解决问题。它不会在悬停时重置。虽然我想知道为什么我的编程方法不起作用。
  • @DJViking 更新了我的解释答案。
  • 感谢您的解释。
  • 我意识到这是一个非常古老的答案,但我希望有人能解释当TitledPane 包含另一个TitledPane 时如何做到这一点。查找“.title”时只选择子窗格而不是父窗格。
  • @Zephyr,您是否尝试过使用 lookupAll 方法。我用示例更新了答案。如果您有不同的情况,请告诉我。
猜你喜欢
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2018-07-07
相关资源
最近更新 更多