gpt4 book ai didi

java - ChoiceBox 背景颜色不会随某些颜色而变化

转载 作者:行者123 更新时间:2023-11-30 06:59:14 24 4
gpt4 key购买 nike

由于某种原因,某些颜色的背景颜色不会改变。对于“黄色”或“粉色”,它不会改变。我能想到的唯一原因是这些是 AWT 颜色,并且它使用它而不是 JavaFX 颜色,但我不确定为什么其他人不这样做。

Here是问题和预期结果的图片

这是我用于更改 ChoiceBox 颜色的代码:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Tooltip;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;
import java.io.InputStream;

public class Clock extends Application {

public Text text;

@Override
public void start(Stage stage) {

DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a");
Date date = new Date();
String stringDate = df.format(date);

text = new Text(10, 60, stringDate);
Font font = null;

InputStream input = getClass().getResourceAsStream("/DIGITALDREAMFAT.ttf");

try {
font = Font.loadFont(input, 30);
} catch (Exception e) {
e.printStackTrace();
}

text.setFont(font);
text.setFill(Color.RED);


Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0), new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent actionEvent) {
Date update = new Date();
String stringNewDate = df.format(update);
text.setText(stringNewDate);
}
}), new KeyFrame(Duration.seconds(1)));

timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();

ChoiceBox colorChoice = new ChoiceBox(FXCollections.observableArrayList("Red", "Blue", "Green", "Yellow", "Pink", "Grey", "Black", "White"));

colorChoice.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e) {
if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Red")) {
colorChoice.setStyle("-fx-base: red");
text.setFill(Color.RED);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Blue")) {
colorChoice.setStyle("-fx-base: blue");
text.setFill(Color.BLUE);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Green")) {
colorChoice.setStyle("-fx-base: green");
text.setFill(Color.GREEN);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Grey")) {
colorChoice.setStyle("-fx-base: grey");
text.setFill(Color.GREY);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("White")) {
colorChoice.setStyle("-fx-base: white");
text.setFill(Color.WHITE);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Black")) {
colorChoice.setStyle("-fx-base: black");
text.setFill(Color.BLACK);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Yellow")) {
colorChoice.setStyle("-fx-base: yellow");
text.setFill(Color.YELLOW);
} else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Pink")) {
colorChoice.setStyle("-fx-base: pink");
text.setFill(Color.PINK);
} else {
colorChoice.setStyle("-fx-base: red");
text.setFill(Color.RED);
}
}
});

HBox hbox = new HBox(colorChoice);
Scene scene = new Scene(new Group(text, hbox));


hbox.setSpacing(10);

colorChoice.setTooltip(new Tooltip("Select Text Colour"));
colorChoice.getSelectionModel().selectFirst();


scene.setFill(Color.TRANSPARENT);

stage.setScene(scene);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setX(0);
stage.setY(0);
stage.setWidth(710);
stage.setHeight(80);
stage.show();
}

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

最佳答案

下拉菜单的默认颜色是查找的颜色-fx-control-inner-background:这又默认为-fx-的非常轻的版本base(比 -fx-base 轻 80%)。因此,如果您从 -fx-base 开始使用浅色,例如黄色,它将有效地完全变亮到接近白色。如果您将选择框替换为-fx-control-inner-background,您将获得所需的效果。

请注意,您可以摆脱可笑的 if-else 构造,因为选择框中的所选项目包含您需要的所有信息。

    ChoiceBox<String> colorChoice = new ChoiceBox<>(
FXCollections.observableArrayList("Red", "Blue", "Green", "Yellow", "Pink", "Grey", "Black", "White"));

colorChoice.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {

String choice = colorChoice.getSelectionModel().getSelectedItem() ;
if (choice != null) {
String color = choice.toLowerCase();
colorChoice.setStyle("-fx-base: "+color+"; -fx-control-inner-background: -fx-base ;");
text.setFill(Color.web(color));
}
}
});

关于java - ChoiceBox 背景颜色不会随某些颜色而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41224392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com