如何在JavaFX中传递参数 [英] How to pass arguments in JavaFX

查看:4088
本文介绍了如何在JavaFX中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这有点混乱,但这里是事情:



我创建了一个与SceneBuilder的窗口,并创建了控制器和一切,窗口。
按钮setOnAction()方法使程序打开另一个窗口,事情是,这个其他窗口是另一个类,我想传递信息到这个其他窗口,但它似乎我不能。下面是一些代码示例:



MainWindow:

  confirm.setOnAction event-> {
try {
LibraryWindowController lwc = new LibraryWindowController();
lwc.setDay(day.getValue());
lwc.setMonth(month.getValue );
lwc.setYear(Integer.parseInt(year.getText()));
lwc.setClient(login.getText());
lwc.start );
} catch(Exception e){
System.out.println(An error has occurredured!);
}
}

LibraryWindowController(变量和设置器):

  private int day,month,year; 
private String client;

public void setDay(int day){
this.day = day;
}

public void setMonth(int month){
this.month = month;
}

public void setYear(int year){
this.year = year;
}

public void setClient(String login){
this.client = login;事实是,在Controller的initialize方法中,有一个System.out。这是一个非常简单的方法。 println(client);
,问题是输出为null,那么为什么会发生这种情况,以及如何解决呢?

解决方案

我建议你使用 fx:root 构造来防止这样的问题。我发现它有助于大大的结构化你的应用程序。它在 FXML文档。如果使用这种方法,所有的屏幕/窗格都是JavaFX中一些Pane类的简单子类。并且因为FXML加载被封装在类中,你可以简单地使用构造函数来创建新的实例(请参阅我链接到的文档中的示例),并像设置任何对象一样设置它们。



例如(假设MyPane是使用此方法设计的窗格):

  MyPane mp = new MyPane(); 
mp.setWhatever(whatever);
Stage newWindow = new Stage();
newWindow.setScene(new Scene(mp));
newWindow.show();

您必须在SceneBuilder中更改的所有内容都是删除您的控制器类(您不再需要它,MyPane类



有关如何使用 fx:root

的完整示例, code>,请参阅我为学生制作的示例。它有完整的源代码(在GitHub上)。



对于你的代码:你调用LibraryWindowController一个控制器,但从它的外观,我认为它是一个应用程序?您不需要创建单独的应用程序来创建多个窗口。只需创建一个具有场景和根节点的新阶段。但是,您使用的步骤应该工作,即使他们不推荐。我猜想 login.getText()返回null或者你的代码中有其他错误。


So, it's a little confusing but here is the thing:

I created a window with SceneBuilder, and created the controller and everything, there is a button in the window. The button setOnAction() method makes the program open another window, the thing is, this other window is another class and I want to pass information to this other window, but it seems I cannot. Here's some code example:

MainWindow:

confirm.setOnAction(event->{
    try {
        LibraryWindowController lwc = new LibraryWindowController();
        lwc.setDay(day.getValue());
        lwc.setMonth(month.getValue());
        lwc.setYear(Integer.parseInt(year.getText()));
        lwc.setClient(login.getText());
        lwc.start(new Stage());
    } catch (Exception e) {
        System.out.println("An error has occured!");
    }
});

LibraryWindowController (variables and setters):

private int day, month, year;
private String client;

public void setDay(int day){
    this.day = day;
}

public void setMonth(int month){
    this.month = month;
}

public void setYear(int year){
    this.year = year;
}

public void setClient(String login){
    this.client = login;
}

The thing is, inside the Controller's initialize method there is a System.out.println(client); and the problem is that the output is null, so, why is that happening and how to fix it?

解决方案

I recommend you use the fx:root construct to prevent issues like this. I find it helps greatly with structuring your app. It is described in the Custom Components section of the FXML docs. If you use this approach, all your screens/panes are simple subclasses of some Pane class in JavaFX. And because the FXML loading is encapsulated in the class, you can simply use the constructor to create new instances (see the example in the documentation I linked to) and set them up as you would any object.

For example (assume MyPane is a Pane you designed using this approach):

MyPane mp = new MyPane();
mp.setWhatever(whatever);
Stage newWindow = new Stage();
newWindow.setScene(new Scene(mp));
newWindow.show();

All you have to change in SceneBuilder is remove your controller class (you no longer need it, class MyPane itself becomes the controller) and check the "Use fx:root" checkbox.

For a complete example of how to use fx:root, see this example I made for my students. It comes with the complete source code (on GitHub).

With regard to your code: you call LibraryWindowController a controller, but from the looks of it I assume it's an Application? You don't need to create separate applications to create multiple windows. Simply create a new stage with a scene and a root node. However, the steps you used should have worked, even if they aren't recommended. I'm guessing either login.getText() returns null or you have an error elsewhere in your code.

这篇关于如何在JavaFX中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆