JavaFX / CSS:鼠标悬停时变亮按钮的原始颜色

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

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

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

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

第二个问题:甚至还有一种方法可以通过css来指定鼠标悬停时按钮的颜色应保留其rgb值,并仅添加0.7值?

谢谢!

iCMS 回答:JavaFX / CSS:鼠标悬停时变亮按钮的原始颜色

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

e只是定义的层次。 G。 FXML文件中使用的一个控件的背景颜色样式命令将始终覆盖CSS文件中使用的相同控件的样式命令。这与您拥有带有设置的#id和设置的.class用于控件的纯CSS的行为相同。 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,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,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);",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,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" />
本文链接:https://www.f2er.com/2262364.html

大家都在问