【问题标题】:How to change a pane color from a different class in javaFX如何从 javaFX 中的不同类更改窗格颜色
【发布时间】: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


    【解决方案1】:

    您不需要单独的课程来满足您的要求。文本字段的textProperty() 上的简单更改侦听器应该足够好了。

    textField.textProperty().addListener((ob, oldValue, newValue) -> {
         if (isNumeric(newValue)) {
             scene.setFill(Color.AQUA);
         } else {
             scene.setFill(Color.FIREBRICK);
         }
    });
    

    这里,isNumeric() 只是我写的一个私有方法。

    private boolean isNumeric(String str) {
        for(Character ch : str.toCharArray()){
            if(Character.isAlphabetic(ch)){
                return false;
            }
        }
        return true;
    }
    

    MVCE

    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.VBox;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    
    public class TextFieldBindSceneColor extends Application {
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            TextField textField = new TextField();
            VBox box = new VBox(textField);
            box.setAlignment(Pos.CENTER);
            box.setStyle("-fx-background-color: transparent");
            Scene scene = new Scene(box, 200, 200);
    
            textField.textProperty().addListener((ob, oldValue, newValue) -> {
                if (isNumeric(newValue)) {
                    scene.setFill(Color.AQUA);
                } else {
                    scene.setFill(Color.FIREBRICK);
                }
            });
            //scene.setFill(Color.RED);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        private boolean isNumeric(String str) {
            for(Character ch : str.toCharArray()){
                if(Character.isAlphabetic(ch)){
                    return false;
                }
            }
            return true;
        }
    }
    

    【讨论】:

    • 谢谢!非常感谢,小新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2015-04-05
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多