我尝试开发一个应用程序,我在Eclipse上工作。
首先我处理了四个文件:
Main.java ,使用 sheet1.fxml 文件启动界面
MyController.java ,声明按钮和fxml文件 sheet1 的锚点。该文件实现了一个动作事件:在单击第一个fxml文件(即 sheet1 )的按钮后,进入第二个界面( sheet2.fxml )。
现在我想处理第二个接口sheet2.fxml。我想:
- 根据文件夹中的文件数添加文本
- 创建按钮以进入第三个界面
但我的问题是我该怎么办?
我试图创建第二个控制器来声明文本nbExcel和按钮,然后与 sheet2.fxml 文件建立关系,但我没有看到诀窍。
如何关联运行位于 sheet2 上的组件的事件和新控制器?
我开始在“myController2”上做到这一点:
public class myController2 extends Application {
// How and where to associate that controller with the fxml file "sheet2" ?
public void start2() throws IOException{
Stage primaryStage2 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("sheet1.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage2.setScene(scene);
primaryStage2.show();
}
@FXML
// declaration text in order to count files in folder in sheet2 interface
private Text nbExcel;
// declaration action buttons in sheet2 interface
// To do
// 1 - INITIALISATION
public void initialize(URL location, ResourceBundle resources) {
File a = new File("C:/Controles/Excel");
int b = 0;
for (File file : a.listFiles()) {
if (file.isFile() && (file.getName().endsWith(".xlsx") )) {
b++;
}
}
nbExcel.setText(Integer.toString(b));
}
任何帮助都会非常感激。
谢谢!
答案 0 :(得分:0)
为了连接fxml文件并创建新场景,你可以这样做:
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/application/sheet2.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
编辑:要确认这段代码应该在您在问题中显示的控制器类中。