【问题标题】:How to get and pass a value from a class to another?如何从一个类获取值并将其传递给另一个类?
【发布时间】:2016-11-24 18:55:30
【问题描述】:

我正在尝试创建一个TextField 来获取用户名。我创建了一个包含 UI 组件的新类。

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

public class start extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Name:");
        TextField textField = new TextField();
        HBox hBox = new HBox();
        hBox.getChildren().addAll(label,textField);
        hBox.setSpacing(10);
        Scene scene = new Scene(hBox,300,200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Hello");
        primaryStage.show();
    }
}

我想获取在“Hello”窗口中输入的名称,以便在另一个类中使用它ShowOff

炫耀类

import javafx.application.Application;

public class ShowOff {
    ShowOff() {
        Application.launch(start.class,"myClass");
    }

    public static void main(String[] args)
    {

    }
}

我该如何实现呢?

【问题讨论】:

  • 你想要什么值得到“hello”窗口?
  • 我想要窗口中的名字字符串
  • 您可以轻松地将字符串从ShowOff() 传递到start(),但反之亦然会有点笨拙。我并不是说这是不可能的,但 IMO,这不是正确的方法。
  • 好的,我该怎么做呢?我是新来的,如果你能给我一个合适的例子,那就太好了。两种方式都会好很多

标签: java inheritance javafx


【解决方案1】:

您的整体结构并不真正适合 JavaFX 应用程序生命周期。 JavaFX 应用程序通过调用Application.launch(...) 启动,然后为您创建应用程序类的实例,并调用其start() 方法。不会返回对创建的实例的引用,并且 launch(...) 方法在应用程序退出之前不会返回(因此如果退出的话,它几乎没有用处)。

所以你真的应该将start() 方法视为应用程序的入口点,并让该方法只做启动。

因此,要在启动类中使用“hello”消息从屏幕中获取值,我会执行以下操作:

public class ShowOff extends Application {

    @Override
    public void start(Stage primaryStage) {
        HelloScreen hello = new HelloScreen();
        primaryStage.setScene(new Scene(hello.getView()));

        // show stage and wait until it closes:
        primaryStage.showAndWait();

        String message = hello.getMessage();
        // do something with message...
        System.out.println(message);
    }

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

public class HelloScreen {
    private TextField textField ;
    private VBox view ;

    public HelloScreen() {
        Label label = new Label("Name:");
        textField = new TextField();
        HBox hBox = new HBox(10, label,textField);
        hBox.setAlignment(Pos.CENTER);
        Button button = new Button("Show User Input In Console");

        // On click, close the window:
        button.setOnAction( e -> view.getScene().getWindow().hide());

        view = new VBox(10, hBox, button);
        view.setAlignment(Pos.CENTER);
    }

    public String getMessage() {
        return textField.getText();
    }

    public Parent getView() {
        return view ;
    }
}

【讨论】:

    【解决方案2】:

    您可以将标签的引用传递给类。我建议阅读this 了解更多信息。

    但是关于您的代码。以下是我将如何修改它:

      public class Start extends Application {
    
      public static void main(String[] args)  {
          Application.launch(Start.class,"myClass");
      }
    
      @Override
      public void start(Stage primaryStage) throws Exception {
        Label label = new Label("Name:");
        TextField textField = new TextField();
        HBox hBox = new HBox();
        hBox.getChildren().addAll(label,textField);
        hBox.setSpacing(10);
        Scene scene = new Scene(hBox,300,200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Hello");
        primaryStage.show();
    
        textField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
              //Handle the change in the text value here
            }
        });
      }
    }
    

    现在在这种情况下,我删除了您的 ShowOff 课程,但如果您需要澄清,请告诉我。

    【讨论】:

      【解决方案3】:

      窗口中的TextField 捕获用户输入。您可以从 TextField 获取文本并将其显示在您想要的任何位置。为了从 TextField 中获取当前文本,您需要使用getText()

      为了学习和理解控件和布局的工作原理,我强烈建议您阅读Getting Started with JavaFX Sample Applications

      这是一个小例子,它使用 TextField 来接受用户输入并在按下按钮时将文本打印到控制台:

      import javafx.application.Application;
      import javafx.geometry.Pos;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.control.TextField;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      
      public class PassParameter extends Application {
      
          @Override
          public void start(Stage primaryStage) throws Exception {
              Label label = new Label("Name:");
              TextField textField = new TextField();
              HBox hBox = new HBox(10, label,textField);
              hBox.setAlignment(Pos.CENTER);
              Button button = new Button("Show User Input In Console");
              // On click, print the text from the TextField in the console
              button.setOnAction( e -> System.out.println(textField.getText()));
              VBox vBox = new VBox(10, hBox, button);
              vBox.setAlignment(Pos.CENTER);
              Scene scene = new Scene(vBox,300,200);
              primaryStage.setScene(scene);
              primaryStage.setTitle("Hello");
              primaryStage.show();
          }
      
          public static void main(String[] args) {
              Application.launch();
          }
      }
      

      【讨论】:

      • 但是我需要接受用户在“Hello”窗口中给出的输入,我该怎么做?
      • 啊,我明白了。您需要在 Hello 窗口中捕获用户输入并将其显示在某处(可能在控制台中打印)。这就是你想要的吗?
      • 是的,我需要在控制台中显示的值
      • @IAmBlake 更新示例。
      【解决方案4】:

      要将值从一个类传递到另一个类,您需要在第一个类中声明变量,然后在另一个类中扩展第一个类

          class PassingVariables
      {
          static int hello;
          public static void main( String[] args )
          {
              ShowOff pass = new ShowOff();
      
          }
      }
      class ShowOff extends PassingVariables
      {
          ShowOff()
          {
              hello = 15;
              System.out.println("Hello = "+hello);
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-03-10
        • 2021-06-19
        • 2018-02-05
        • 2016-04-05
        • 2016-03-08
        • 2014-02-24
        • 2013-03-16
        • 1970-01-01
        相关资源
        最近更新 更多