【问题标题】:JavaFX/CSS: Brighten button's original color on mouse-overJavaFX/CSS:在鼠标悬停时使按钮的原始颜色变亮
【发布时间】:2020-05-27 11:55:52
【问题描述】:

我有各种按钮,每个按钮都有自己的背景颜色(-fx-background-color: rgb (xxx,xxx,xxx)。按钮的颜色在 .fxml 文件中定义。

现在我想在 .css 文件中定义每个按钮的背景颜色,以便在鼠标悬停时变亮。

例如:Button1 的常规颜色是 -fx-background-color: rgb(176,30,0) 在鼠标悬停时,它应该更改为 -fx-background-color: rgba(176,30,0,0.7)

我的第一个问题:fxml 文件中定义的 -fx-background-color 覆盖了 .css 中定义的 .button:hover{-fx-background-color: rgba(176,30,0,0.7);}文件。

第二个问题:有没有办法通过 css 指定按钮的颜色在鼠标悬停时应该保留其 rgb 值,并且只添加 0.7 的值?

提前致谢!

【问题讨论】:

  • 您要变亮颜色还是要更改不透明度?因为对于前者,您可以使用查找颜色和derive——请参阅JavaFX CSS 参考指南color section。不幸的是,我不相信只有在保持颜色的同时改变不透明度的功能。虽然你可以使用 anko 的解决方案。

标签: css javafx fxml


【解决方案1】:

我的第一个问题:[...]

e 只是定义的层次结构。 G。在 FXML 文件中创建的一个控件的背景颜色样式命令将始终覆盖在 CSS 文件中创建的同一控件的样式命令。它的行为就像你有一个纯 CSS 和一个 set #id 和一个 set .class 作为控件一样。 e。 G。在 id 语句中定义的背景颜色将覆盖为类定义的背景颜色。所以这是标准行为,你不能改变它。

第二个问题:[...]

没有像“-fx-background-transparency: 0.7;”这样的 CSS 命令。您可以使用 CSS 来执行此操作(并且无需在 FXML 中覆盖):

CSS 文件:

.my-btn-class {
    -fx-background-color: rgb(176, 30, 0);
}

.my-btn-class:hover {
    -fx-background-color: rgba(176, 30, 0, 0.7);
}

FXML 文件:

  <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.Button?>


    <Button styleClass="my-btn-class" stylesheets="@styling.css" text="Button" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" />

或者你可以这样做:

控制器类:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Controller implements Initializable {

    @FXML
    private Button button;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        button.hoverProperty().addListener(((observable, oldValue, newValue) -> makeButtonTransparent(button, newValue)));
    }

    private void makeButtonTransparent(Button button, boolean transparent) {

        // Get the current style statements:
        String currentStyle = button.getStyle();

        // Check if there is a styling statement for background color with rgb or rgba:
        Pattern pattern = Pattern.compile("-fx-background-color: rgb(a?)\\(([\\d,\\s.])*\\);");
        Matcher matcher = pattern.matcher(currentStyle);

        String currentBackgroundColorStyle;
        if (matcher.find()) {
            // Extract the existing background color statement:
            currentBackgroundColorStyle = currentStyle.substring(matcher.start(), matcher.end());
        } else
            // No statement for background color in rgb(a) found:
            return;

        // Get the rgb values from the string:
        int[] rgb = new int[3];
        matcher = Pattern.compile("\\d{1,3}").matcher(currentBackgroundColorStyle);

        for (int i = 0; i < 3; i++) {
            if (matcher.find())
                rgb[i] = Integer.parseInt(currentBackgroundColorStyle.substring(matcher.start(), matcher.end()));
        }

        if (transparent)
            // Replace the background color statement with transparency value:
            button.setStyle(currentStyle.replace(currentBackgroundColorStyle, String.format("-fx-background-color: rgba(%d, %d, %d, 0.7);", rgb[0], rgb[1], rgb[2])));
        else
            // Replace the background color statement without transparency value:
            button.setStyle(currentStyle.replace(currentBackgroundColorStyle, String.format("-fx-background-color: rgb(%d, %d, %d);", rgb[0], rgb[1], rgb[2])));
    }
}

FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>


<Button fx:id="button" style="-fx-background-color: rgb(176, 30, 0); -fx-border-color: blue;" text="Button" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller" />

【讨论】:

  • 非常感谢!有没有一种简单的方法可以让您的第二个变体与任何按钮一起使用,而不必为每个按钮分配唯一的 -fx:id?
  • 是的,您可以使用 Hans Brendes 的两种方法回答:stackoverflow.com/questions/24986776/… 然后您可以添加到您的初始化方法:getAllNodes(root).stream() .filter(node -> node instanceof Button) .map(node -> (Button) node) .filter(button -> !button.getStyle().isEmpty()) .forEach(button -> button.hoverProperty().addListener(((observable, oldValue, newValue) -> makeButtonTransparent(button, newValue))));
  • 我希望我不会在后续问题上占用您太多时间,但遵循您的建议会导致一个或多个空指针异常。我假设因为 root 没有在控制器类中定义,仅在单独的 Main 类中定义。我将如何在控制器中初始化 root?
  • 没关系,问吧。 “root”是指约束按钮的父节点(基本布局,例如 GridPane)。如果您使用 FXML 文件,它将是
  • 它来自监听器:button.hoverProperty().addListener(((observable, oldValue, newValue) -> [...] 当用户将鼠标悬停在(在这种情况下)按钮,newValue 将为真,如果鼠标光标离开按钮 newValue 将为假。
猜你喜欢
  • 1970-01-01
  • 2016-10-30
  • 2013-09-10
  • 2012-08-16
  • 2016-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
相关资源
最近更新 更多