JavaFX 根据输入更改按钮的颜色

问题描述 投票:0回答:1

我目前正在使用 javaFX 构建可视化工具。您可以在下图中看到有文本框和绿色按钮。对于每个显示 0 的文本框,它都链接到其下方的特定按钮。但是,只有当输入值为 1 时,该按钮才应变为绿色。如果输入为 0,该按钮应为白色;如果输入为 1,则该按钮应为白色。如果是任何其他数字,应该会弹出错误消息。

enter image description here场景构建器预览

我尝试在 Controller.java 中创建一个类,该类将从 scenebuilder 中获取 fx:id,但这不起作用。我正在尝试找到一种方法,可以为每个文本框使用特定名称,因为每个文本框都链接到不同的按钮。

javafx user-input scenebuilder
1个回答
0
投票

您可以做的一件事是创建一个

ChangeListener
来处理所有逻辑。另一种是您可以为每个
ChangeListener
创建不同的
TextField

所有

ChangeListener
TextFields
示例。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;

public class App extends Application {
    
    TextField tfOne = new TextField();
    TextField tfTwo = new TextField();
    Button btnOne = new Button("One");
    Button btnTwo = new Button("Two");
    
    @Override
    public void start(Stage stage){        
        VBox root = new VBox();
        
        HBox topRoot = new HBox(tfOne, tfTwo);
        topRoot.setSpacing(3);
        
        HBox bottomRoot = new HBox(btnOne, btnTwo);
        bottomRoot.setSpacing(3);        
        
        tfOne.textProperty().addListener(textFieldChangeListener);
        tfTwo.textProperty().addListener(textFieldChangeListener);
        
        root.getChildren().addAll(topRoot, bottomRoot);
        Scene newScene = new Scene(root);
        stage.setWidth(200);
        stage.setHeight(200);
        stage.setScene(newScene);
        stage.show();
    }

    ChangeListener<String> textFieldChangeListener =    (observable, oldValue, newValue) -> {
                                                            String style;
                                                            
                                                            switch (newValue) {
                                                                case "":
                                                                    style = "";
                                                                    break;
                                                                case "0":
                                                                    style = "-fx-background-color: white;";
                                                                    break;
                                                                case "1":
                                                                    style = "-fx-background-color: green;";
                                                                    break;
                                                                default:
                                                                    style = "-fx-background-color: red;";
                                                            }
                                                            TextField currentTextField = ((TextField)((StringProperty)observable).getBean());
                                                           
                                                            if(currentTextField == tfOne)
                                                            {
                                                                btnOne.setStyle(style);
                                                            }
                                                            else if(currentTextField == tfTwo)
                                                            {
                                                                btnTwo.setStyle(style);
                                                            }
                                                            else
                                                            {
                                                                System.out.println("error!");
                                                            }
                                                        };
    
    public static void main(String[] args) {
        launch();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.