我正在構建一個JavaFX應用程序,並且想要訪問在JavaFX UI中作爲參數傳遞的值。出於某種原因,我無法在除基本方法launchForm之外的所有方法中訪問這些值。這是我的代碼的樣子。在JavaFX方法中使用參數
public class FormBuilder extends Application {
/*
* (non-Javadoc)
* @see javafx.application.Application#start(javafx.stage.Stage)
* Scene scene
* Group root
* BorderPane borderPane
* TabPane tabPane
* Tab stocksTab
* BorderPane stockTabBorderPane
* GridPane gridPane
*
*/
private Stocks stockData = new Stocks();
private int size;
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Stock Manager");
Group root = new Group();
Scene scene = new Scene(root, 1024, 800, Color.WHITE);
TabPane tabPane = new TabPane();
BorderPane borderPane = new BorderPane();
BorderPane stockTabBorderPane = new BorderPane();
Tab stocksTab = new Tab("Stocks");
stockTabBorderPane.setTop(this.addHBox());
stockTabBorderPane.setCenter(this.createGridPane());
stocksTab.setContent(stockTabBorderPane);
tabPane.getTabs().add(stocksTab);
borderPane.setCenter(tabPane);
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
root.getChildren().add(borderPane);
stage.setScene(scene);
stage.show();
}
private HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #336699;");
Button buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
private GridPane createGridPane() {
GridPane gridPane = new GridPane();
gridPane.setLayoutX(39);
gridPane.setLayoutY(131.0);
gridPane.setAlignment(Pos.TOP_CENTER);
gridPane.setVgap(5.0);
gridPane.setHgap(10.0);
gridPane.add(new Label("Active"), 1,1);
gridPane.add(new Label("Stock"), 2, 1);
gridPane.add(new Label("Symbol"), 3, 1);
gridPane.add(new Label("LPP"), 4, 1);
gridPane.add(new Label("LPP"), 5, 1);
gridPane.add(new Label("HPP"), 6, 1);
gridPane.add(new Label("LTP"), 7, 1);
System.out.println(this.size);
for(int v=2;v < this.stockData.getStocks().size()+2; v++) {
gridPane.add(new CheckBox(), 1, v);
gridPane.add(new Label("Amazon"), 2, v);
gridPane.add(new TextField(), 3,v);
gridPane.add(new TextField(), 4,v);
gridPane.add(new TextField(), 5,v);
gridPane.add(new TextField(), 6,v);
gridPane.add(new TextField(), 7,v);
}
return gridPane;
}
public void launchForm(Stocks stockData) {
this.stockData = stockData;
this.size = stockData.getStocks().size();
System.out.println(stockData.getStocks().size());
System.out.println(stockData.getStocks().get(0).getSector());
launch();
}
}
現在的問題是,當我嘗試和createGridPane
方法中的stockData
對象下訪問任何值,該值是不可用的。
實例是
this.stockData.getStocks().size()
給出的0在createGridPane
方法的值。但它在launchForm
方法中的值爲2。
同樣有像
this.stockData.getStocks().get(0).getSector()
返回在launchForm
方法的值"Retail"
其它值。但是當我嘗試在同一個類中以不同的方法訪問它時,出現異常。
有人可以幫我嗎?
你從哪裏調用'launchForm'?你爲什麼要從該方法中調用'launch()'? ['launch()'](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#launch-java.lang.String...-)啓動應用程序:當然,應用程序已經開始了。我認爲你必須從根本上誤解了JavaFX的生命週期:請參閱['Application' docs](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html) –
我想你可能需要在此問題中包含更多信息,以瞭解您正在做什麼以及如何構建應用程序。 –