Java ChoiceBox背景色不会随某些颜色而改变

Java ChoiceBox背景色不会随某些颜色而改变,java,javafx,Java,Javafx,由于某些原因,某些颜色的背景颜色不会改变。它不会变为“黄色”或“粉色”。我能想到的唯一原因是,这些是AWT颜色,它使用AWT颜色而不是JavaFX颜色,但我不知道为什么其他人不会这样做 是对问题和预期结果的描述 这是我的ChoiceBox颜色更改代码: import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; imp

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

是对问题和预期结果的描述

这是我的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);
            }
    }
导入javafx.application.application;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入javafx.collections.FXCollections;
导入javafx.scene.control.ChoiceBox;
导入javafx.scene.control.Tooltip;
导入javafx.stage.StageStyle;
导入javafx.scene.paint.Color;
导入javafx.scene.layout.HBox;
导入javafx.scene.text.Font;
导入javafx.scene.text.text;
导入java.text.DateFormat;
导入java.util.Date;
导入java.text.simpleDataFormat;
导入javafx.animation.animation;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.util.Duration;
导入java.io.InputStream;
公共类时钟扩展应用{
公共文本;
@凌驾
公众假期开始(阶段){
DateFormat df=新的简化格式(“EEE,MMM d yyyy-h:mm:ss a”);
日期=新日期();
字符串stringDate=df.format(日期);
文本=新文本(10,60,stringDate);
Font=null;
InputStream输入=getClass().getResourceAsStream(“/DIGITALDREAMFAT.ttf”);
试一试{
font=font.loadFont(输入,30);
}捕获(例外e){
e、 printStackTrace();
}
text.setFont(字体);
text.setFill(颜色为红色);
Timeline Timeline=新的时间线(新的关键帧(持续时间.秒(0),新的EventHandler()){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
日期更新=新日期();
字符串stringNewDate=df.format(更新);
text.setText(stringNewDate);
}
}),新的关键帧(持续时间。秒(1));
timeline.setCycleCount(Animation.unfinite);
timeline.play();
ChoiceBox colorChoice=新的ChoiceBox(FXCollections.observableArrayList(“红色”、“蓝色”、“绿色”、“黄色”、“粉色”、“灰色”、“黑色”、“白色”));
setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent e){
if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“红色”)){
颜色选择。设置样式(“-fx基:红色”);
text.setFill(颜色为红色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“蓝色”)){
颜色选择。设置样式(“-fx基:蓝色”);
text.setFill(颜色为蓝色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“绿色”)){
colorChoice.setStyle(“-fx base:green”);
text.setFill(颜色为绿色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“灰色”)){
颜色选择。设置样式(“-fx基:灰色”);
text.setFill(颜色为灰色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“白色”)){
颜色选择。设置样式(“-fx基:白色”);
text.setFill(颜色为白色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“黑色”)){
颜色选择。设置样式(“-fx基:黑色”);
text.setFill(颜色为黑色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“黄色”)){
colorChoice.setStyle(“-fx base:黄色”);
text.setFill(颜色为黄色);
}else if(colorChoice.getSelectionModel().getSelectedItem().toString().equals(“粉色”)){
颜色选择。设置样式(“-fx基:粉色”);
text.setFill(颜色为粉红色);
}否则{
颜色选择。设置样式(“-fx基:红色”);
text.setFill(颜色为红色);
}
}
});
HBox HBox=新的HBox(颜色选择);
场景=新场景(新组(文本,hbox));
hbox.setspace(10);
设置工具提示(新工具提示(“选择文本颜色”);
colorChoice.getSelectionModel().selectFirst();
场景.设置填充(颜色.透明);
舞台场景;
stage.initStyle(StageStyle.TRANSPARENT);
setX(0);
stage.setY(0);
舞台布景宽度(710);
舞台设置高度(80);
stage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

这里您正在收听的是控件本身,而不是项目,并且更改不会发生,它总是经过else条件,因此当您单击控件时,它将默认选择红色(未选择任何项目),因此如果您要更改颜色
cb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {

        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {

            cb.setStyle("-fx-base:" + newValue.toLowerCase() + ";");


        }
    });
    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));
            }
        }
    });