我的第一个问题:[...]
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