TextField中的返回值

时间:2018-05-20 15:09:50

标签: java javafx

我正在尝试在我的JavaFX应用程序中返回IP地址但是我不确定我做得对。我在java中有点生疏(自从我上次为任何大学申请等写了两年以来)。有人能够在尝试将返回的主机地址放入文本字​​段方面告诉我哪里出错了。区域以**

突出显示
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();

    }

}

1 个答案:

答案 0 :(得分:0)

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

#include <stdio.h>
#include <stdlib.h>
int main (){
    int n ;
    int arr1[n];
    long long int arrcount ;
    scanf("%d" , n);
    for(int i = 0; i <= n; i++) {
        scanf("%d" , &arr1[n]);
    }
    for( int i = 0; i <=n; i++) {
        arrcount = arrcount + arr1[n];
    }
    printf("%li" , arrcount);
}