2016-11-24 8 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で使用するために、「Hello」ウィンドウに入力した名前を取得したいとします。

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()'に文字列を簡単に渡すことができますが、その逆も少し不便です。私はそれが可能ではないが、IMO、それは正しい方法ではないと言っているわけではありません。 – 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

しかし、私は "Hello"ウィンドウでユーザーによって与えられた入力を取る必要があります、どのように私はそれを行うことはできますか? – IAmBlake

+0

ああ、そうです。あなたはHelloウィンドウでユーザー入力を取得し、どこかに表示する必要があります(コンソールに表示されることがあります)。それはあなたが欲しいものですか? – ItachiUchiha

+0

ええ、私はコンソールに表示する値が必要です – IAmBlake

-1

:ここ

は、ユーザの入力を受け付けるようにTextFieldを使用し、ボタンを押すだけで、コンソールにテキストを出力する小さな例です最初のクラスで変数を宣言してから、他のクラスで最初のクラスを拡張する

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アプリケーションのライフサイクルに実際には適合しません。 JavaFXアプリケーションは、Application.launch(...)を呼び出して起動し、アプリケーションクラスのインスタンスを作成し、start()メソッドを呼び出します。作成されたインスタンスへの参照は返されず、アプリケーションが終了するまでlaunch(...)メソッドが返されません(そうした場合にはほとんど使用されません)。

実際に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 ; 
    } 
} 
関連する問題