[英]How to get and pass a value from a class to another?
我正在嘗試創建一個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)
{
}
}
我該如何實施?
您的總體結構實際上並不適合JavaFX Application生命周期。 通過調用Application.launch(...)
啟動JavaFX應用程序,然后為您創建該應用程序類的實例,並調用其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 ;
}
}
您可以將標簽的引用傳遞給類。 我建議閱讀此內容以獲取更多信息。
但是關於您的代碼。 這是我的修改方法:
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
類,但是如果您需要澄清,請告訴我。
窗口中的TextField捕獲用戶輸入。 您可以從TextField中獲取文本,並將其顯示在所需的任何位置。 為了從TextField獲取當前文本,您需要使用getText()
。
為了學習和理解控件和布局的工作方式,我強烈建議您閱讀《 JavaFX示例應用程序入門》 。
這是一個小示例,該示例使用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();
}
}
要將值從一個類傳遞到另一個類,您需要在第一個類中聲明變量,然后在另一個類中擴展第一個類
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.