简体   繁体   English

为什么不在NetBeans控制台中打印用户输入? JavaFX

[英]Why is not printing user input in NetBeans console? Javafx

I need to print user input in my console (Javafx NetBeans). 我需要在控制台(Javafx NetBeans)中打印用户输入。

This is my code, and strangely is only printing the label name: "Address". 这是我的代码,奇怪的是仅打印标签名称:“ Address”。 Earlier when I only had 2 fields it would only print the last entry, and it would not print the first entry by user when button pressed. 早些时候,当我只有2个字段时,它只会打印最后一个条目,而当按下按钮时,它不会打印出用户的第一个条目。

How can I print all the input from user in console? 如何在控制台中打印用户的所有输入?

    package customerentry2;
import javafx.geometry.Insets;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


/**
 *
 * @author 718358
 */

public class CustomerEntry2 extends Application {
    Stage window;
    Scene scene;
    Button button;

    public static void main(String[] args) {
        launch(args);
    }

       @Override
    public void start(Stage primaryStage) throws Exception 
    {
        window = primaryStage;
        window.setTitle("Customer Entry");

        Label nameLabel = new Label("First Name: ");
        Label nameLabel2 = new Label("Last Name: ");
        Label addressInput = new Label("Address: ");
        TextField nameInput = new TextField();
        TextField nameInput2 = new TextField();
        TextField addressInput3 = new TextField();
        button = new Button("Save");
        button.setOnAction(e -> System.out.println(nameInput.getText()));
        button.setOnAction(e -> System.out.println(nameInput2.getText()));
        button.setOnAction(e -> System.out.println(addressInput3.getText()));
        //Layout
        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20, 20, 20, 20));
        layout.getChildren().addAll(nameLabel, nameInput, nameLabel2, nameInput2, addressInput, addressInput3,  button);

        scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();

    }

    /**
     * @param args the command line arguments
     */


}

The problem is happening because each time you call setOnAction it replaces the existing EventHandler , rather than adding an additional one. 之所以发生此问题,是因为每次调用setOnAction它都会替换现有的EventHandler ,而不是添加一个额外的EventHandler There's two ways you can fix this. 有两种方法可以解决此问题。

You can either take care of all three println s in one EventHandler , like this: 您可以在一个EventHandler中处理所有三个println ,如下所示:

button.setOnAction(e -> {System.out.println(nameInput.getText());
                         System.out.println(nameInput2.getText()));
                         System.out.println(addressInput3.getText());
                        });

Or you can use addEventHandler to add more EventHandler s to the button without replacing the existing ones. 或者,您可以使用addEventHandler将更多EventHandler添加到按钮中,而无需替换现有按钮。 That would look like this: 看起来像这样:

button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(nameInput.getText()));
button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(nameInput2.getText()));
button.addEventHandler(ActionEvent.ACTION,
                       (ActionEvent e) -> System.out.println(addressInput3.getText()));

Either one should work for you. 任何一种都应该为您工作。 The first one is a little shorter and easier to read, but the second way is more flexible if you plan to dynamically add and remove the handlers. 第一种方法更短并且更易于阅读,但是如果您打算动态添加和删除处理程序,则第二种方法更灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM