2016-11-24 39 views
0

我正在嘗試創建一個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(); 
    } 
} 

我想在「你好」窗口中輸入的名稱在其他類ShowOff使用它。

ShowOff類

import javafx.application.Application; 

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

    public static void main(String[] args) 
    { 

    } 
} 

我怎麼能實現呢?

+0

你想要什麼樣的價值得到「你好」窗口? – ItachiUchiha

+0

我想要窗口中的名稱字符串 – IAmBlake

+0

您可以很容易地將'ShowOff()'中的字符串傳遞給'start()',但反過來會有點笨拙。我不是說這不可能,但是海事組織,這不是正確的做法。 – ItachiUchiha

回答

0

您可以將標籤的引用傳遞給該類。我建議閱讀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類,但如果你需要一些澄清,讓我知道。

0

窗口中的TextField捕捉用戶輸入。您可以從TextField獲取文本並將其顯示在任何需要的位置。爲了從TextField獲取當前文本,您需要使用getText()

爲了學習和理解控件和佈局如何工作,我強烈建議您通過Getting Started with JavaFX Sample Applications

這裏是一個使用文本字段接受用戶輸入並打印文本到控制檯上的按鈕的按下一個小例子:

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(); 
    } 
} 
+0

但我需要用戶在「你好」窗口中給出的輸入,我該怎麼做? – IAmBlake

+0

啊,我明白了。您需要在Hello窗口中捕獲用戶輸入並將其顯示在某處(可以在控制檯中打印)。那是你要的嗎? – ItachiUchiha

+0

是的,我確實需要在控制檯中顯示的值 – IAmBlake

-1

從一個類中的值傳遞給另一個需要聲明你的第一類變量,然後在其他類擴展的一流

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); 

    } 
} 
1

你的整體結構並沒有真正融入JavaFX應用程序的生命週期。通過調用Application.launch(...)啓動JavaFX應用程序,然後爲您創建應用程序類的實例,並調用其start()方法。不會返回創建的實例的引用,並且在應用程序退出之前方法不會返回(因此,如果有的話,它將不會有什麼用處)。

因此,您應該真的考慮將start()方法作爲應用程序的入口點,並且讓該方法不過是啓動而已。

所以在啓動類的「你好」消息來從屏幕上的價值,我會做這樣的事情:

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 ; 
    } 
} 
相關問題