2016-11-24 81 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 ; 
    } 
} 
相关问题