gpt4 book ai didi

Java fx 改变字符串的颜色

转载 作者:行者123 更新时间:2023-12-02 10:34:38 32 4
gpt4 key购买 nike

在我的程序中,我必须通过单击单选按钮来更改示例字符串的颜色。但每次我点击它们时我都会收到错误。我不断收到此错误。我还使用 e(fx)clipse 来对此进行编码。

Caused by: java.lang.ClassCastException: javafx.graphics@10.0.2/javafx.scene.paint.Color cannot be cast to javafx.graphics@10.0.2/javafx.scene.text.Text at employee.view.MainController.colorRadioButtonSelected(MainController.java:83) package employee.view;

import javafx.beans.value.ChangeListener;
import javafx.scene.paint.Paint;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;

public class MainController {

@FXML
private BorderPane myPane;

@FXML
private RadioButton blackRadioButton;

@FXML
private ToggleGroup colorToggleGroup;

@FXML
private RadioButton redRadioButton;

@FXML
private RadioButton blueRadioButton;

@FXML
private RadioButton greenRadioButton;

@FXML
private ListView<String> mylistView;

@FXML
private CheckBox boldCheckBox;

@FXML
private CheckBox italicCheckBox;

String Text;
Text sample=new Text(50,300,"SAMPLE");
FontWeight weight = FontWeight.NORMAL; // FontWeight.BOLD is boldface
FontPosture posture = FontPosture.REGULAR; // FontPosture.ITALIC is italic
int size=18;
boolean fontBold = false;
boolean fontItalic = false;

public void initialize() {
blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);
myPane.getChildren( ).add( sample );
sample.setFont(Font.font("Verdana", weight, posture, size));
}

@FXML
void boldCheckBoxSelected(ActionEvent event) {
}

@FXML
void colorRadioButtonSelected(ActionEvent event) {
sample= (javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();
}

@FXML
void italicCheckBoxSelected(ActionEvent event) {
}
}

最佳答案

您将数据设置为颜色

blackRadioButton.setUserData(Color.BLACK);
redRadioButton.setUserData(Color.RED);
greenRadioButton.setUserData(Color.GREEN);
blueRadioButton.setUserData(Color.BLUE);

但是你尝试将其转换为文本

(javafx.scene.text.Text) colorToggleGroup.getSelectedToggle().getUserData();

要解决此问题,您可以使用 switch 语句或多个 if 语句来根据选择的颜色设置 sample 对象。 if 语句示例:

if ((Color) colorToggleGroup.getSelectedToggle().getUserData() == Color.BLUE) {
sample = new Text(50, 300, "Something");
}

或者,您可以将单选按钮的数据设置为文本,例如:

blueRadioButton.setUserData(new Text(50, 300, "Something"));

关于Java fx 改变字符串的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384097/

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