use*_*988 0 java javafx colors
我想让用户使用 ColorPicker 选择颜色,然后使用该颜色更改按钮的颜色。JavaFX
ColorPicker cp = new ColorPicker();
cp.setOnAction(e -> {
Color c = cp.getValue();
System.out.println(c);
});
Run Code Online (Sandbox Code Playgroud)
在 println 中,它会给出像 0xe6e64dff,0xccffccff... 这样的答案。
如果我想将按钮涂成蓝色,我需要使用这个:
Button button = new Button();
button.setStyle("-fx-background-color: #ff0000; ");
Run Code Online (Sandbox Code Playgroud)
所以我假设我必须先将颜色值转换为字符串才能使用它?或者我该怎么做?我如何让选择的颜色在 setStyle 行中可用?
Color为十六进制字符串您可以使用以下内容从 a 创建十六进制字符串Color:
private static String toHexString(Color color) {
int r = ((int) Math.round(color.getRed() * 255)) << 24;
int g = ((int) Math.round(color.getGreen() * 255)) << 16;
int b = ((int) Math.round(color.getBlue() * 255)) << 8;
int a = ((int) Math.round(color.getOpacity() * 255));
return String.format("#%08X", (r + g + b + a));
}
Run Code Online (Sandbox Code Playgroud)
这还将包括颜色的 alpha(即不透明度)。请注意,JavaFX CSS 参考指南没有记录对 4 位/8 位十六进制值的任何支持:
RGB 十六进制:十六进制表示法的 RGB 值格式是一个“#”,紧跟三个或六个十六进制字符。三位数 RGB 符号 (#rgb) 通过复制数字而不是通过添加零转换为六位数形式 (#rrggbb)。例如,#fb0 扩展为 #ffbb00。这确保可以使用短符号 (#fff) 指定白色 (#ffffff) 并消除对显示颜色深度的任何依赖。
但是,文档Color#web(String)说支持以下格式:
带有可选十六进制 alpha 通道 的 HTML 长格式或短格式十六进制字符串[强调已添加]。十六进制值可以由前面
"0x"或"#"与可以是范围为2个位数00到0xFF或范围内的单个数字0来F。
还要注意它说的十六进制值可以用前缀或者 0x或#。
这是使用上述实用程序方法的示例:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
ColorPicker picker = new ColorPicker();
root.getChildren().add(picker);
picker.setOnAction(
event -> {
event.consume();
Color value = picker.getValue();
if (value == null) {
root.setStyle(null);
} else {
String style = String.format("-fx-background-color: %s;", toHexString(value));
root.setStyle(style);
}
});
primaryStage.setScene(new Scene(root, 500, 300));
primaryStage.setTitle("Color to Hexadecimal Example");
primaryStage.show();
}
private static String toHexString(Color color) {
int r = ((int) Math.round(color.getRed() * 255)) << 24;
int g = ((int) Math.round(color.getGreen() * 255)) << 16;
int b = ((int) Math.round(color.getBlue() * 255)) << 8;
int a = ((int) Math.round(color.getOpacity() * 255));
return String.format("#%08X", (r + g + b + a));
}
}
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以完成你正在做的事情。您可以直接设置属性style,而不是设置需要将 转换Color为 a 的。下面是一个例子:StringRegion#background
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
ColorPicker picker = new ColorPicker();
root.getChildren().add(picker);
picker.setOnAction(
event -> {
event.consume();
Color value = picker.getValue();
if (value == null) {
root.setBackground(null);
} else {
root.setBackground(new Background(new BackgroundFill(value, null, null)));
}
});
primaryStage.setScene(new Scene(root, 500, 300));
primaryStage.setTitle("Programmatically Set Background Color Example");
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)