【发布时间】:2015-10-26 03:49:42
【问题描述】:
我有一些输入验证,我正在从用户输入文本框的操作中进行。我希望它在输入字母时变为橙色,在输入数字时变为白色。我遇到的问题是,一旦应用程序意识到它是数字或字母,我想不出改变颜色的方法。
我需要它来获取用户的输入,查看 TypedKey 类,然后查看 if、else if 并基于该更改场景中的颜色。这是我能想到的最合乎逻辑的方式,但如果有人知道更有效的方式,或者可以帮助我改进我认为的方式,将不胜感激。
@Override
public void start(Stage primaryStage) throws Exception {
GridPane pane = new GridPane();
pane.setHgap(5);
pane.setVgap(5);
pane.setAlignment(Pos.CENTER);
Scene scene = new Scene(pane, 300, 200);
pane.add(new Label("Salary and Wage"), 0, 0);
pane.add(tfSalaryAndWage, 1, 0);
pane.add(btOK, 1, 1);
//Red color for tfSalary box
Color redColor = Color.rgb( 255, 0 , 0, 0.5);
BackgroundFill backgroundFill = new BackgroundFill(redColor, null, null);
Background background= new Background(backgroundFill);
tfSalaryAndWage.setBackground(background);
TypedKey typedKeyHandler = new TypedKey(this);
tfSalaryAndWage.setOnKeyTyped(typedKeyHandler);
primaryStage.setScene(scene);
primaryStage.setTitle("Loan Calculator");
primaryStage.show();
}
输入验证类
class TypedKey implements EventHandler<KeyEvent> {
ValidateDataTest formObj = null; // Declare a data member to represent an
// object of the form class.
Color blueBackgroundColor = null;
public TypedKey(ValidateDataTest formObj) // This constructor receives an
// object of the form class.
{
this.formObj = formObj;
}
public void handle(KeyEvent e) {
if ((e.getCharacter().compareTo("a") >= 0 && e.getCharacter()
.compareTo("z") <= 0)
|| (e.getCharacter().compareTo("A") >= 0 && e.getCharacter()
.compareTo("Z") <= 0)) {
System.out.println("LOOKS LIKE A Z WAS PRESSED");
}
else if ((e.getCharacter().compareTo("0") >= 0 && e.getCharacter()
.compareTo("9") <= 0)) {
System.out.println("LOOKS LIKE NUMBERS WERE PRESSED");
}
}
}
【问题讨论】:
标签: class javafx colors javafx-2 pane