Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TextField中的返回值_Java_Javafx - Fatal编程技术网

Java TextField中的返回值

Java TextField中的返回值,java,javafx,Java,Javafx,我试图在JavaFX应用程序中返回IP地址,但我不确定我是否正确。我对我的java有点生疏(自从我上次写大学申请书以来已经有2年了),有人能告诉我在尝试将返回的主机地址输入文本字段时哪里出错了吗。突出显示的区域** import java.net.InetAddress; import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.sc

我试图在JavaFX应用程序中返回IP地址,但我不确定我是否正确。我对我的java有点生疏(自从我上次写大学申请书以来已经有2年了),有人能告诉我在尝试将返回的主机地址输入文本字段时哪里出错了吗。突出显示的区域**

import java.net.InetAddress;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {
    Stage window;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("thenewboston - JavaFX");

        //GridPane with 10px padding around edge
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(8);
        grid.setHgap(10);

        //Name Label - constrains use (child, column, row)
        Label nameLabel = new Label("Find my IP Address:");
        nameLabel.setId("bold-label");
        GridPane.setConstraints(nameLabel, 0, 0);

        //Search IP Address 
        Button IPlookupButton =new Button("Search for IP");
        GridPane.setConstraints(IPlookupButton, 1, 0);
        IPlookupButton.setOnAction(event -> {
            try {
                InetAddress thisIp = InetAddress.getLocalHost();
                System.out.println("IP:" + thisIp.getHostAddress());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        **//Name Input
        TextField nameInput = new Textfield(thisIp.getHostAddress());
        GridPane.setConstraints(nameInput, 3, 0);**

        //Password Label
        Label passLabel = new Label("MAC Address Look UP:");
        GridPane.setConstraints(passLabel, 0, 1);

        //Password Input
        TextField passInput = new TextField();
        passInput.setPromptText("password");
        GridPane.setConstraints(passInput, 1, 1);

        //Login
        Button loginButton = new Button("Log In");
        GridPane.setConstraints(loginButton, 1, 2);

        //Sign up
        Button signUpButton = new Button("Sign Up");
        signUpButton.getStyleClass().add("button-blue");
        GridPane.setConstraints(signUpButton, 1, 3);

        //Add everything to grid
        grid.getChildren().addAll(nameLabel, IPlookupButton, nameInput, 
        passLabel, passInput, loginButton, signUpButton);

        Scene scene = new Scene(grid, 600, 600);
        scene.getStylesheets().add("colour.css");
        window.setScene(scene);
        window.show();

    }

}

只需在按钮的处理程序中设置文本字段的文本:

//Search IP Address 
Button IPlookupButton =new Button("Search for IP");
GridPane.setConstraints(IPlookupButton, 1, 0);

//Name Input
TextField nameInput = new TextField();
GridPane.setConstraints(nameInput, 3, 0);

IPlookupButton.setOnAction(event -> {
    try {
        InetAddress thisIp = InetAddress.getLocalHost();
        nameInput.setText("IP:" + thisIp.getHostAddress());
    } catch (Exception e) {
        e.printStackTrace();
    }
});