【发布时间】:2018-09-29 21:35:28
【问题描述】:
我正在尝试从网格开始创建用户界面。我在 scenebuilder 中构建了网格,现在我想使用我的控制器来添加列和行。但是,我的程序似乎没有在我的控制器中运行 initialise(),因为网格不会改变大小。 这是我的主要课程:
import javafx.application.Application;
import javafx.fxml.FXMLLoader; import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
//@Override
public void start(Stage primaryStage) {
try {
int width = 7;
int height = 7;
final FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("GUI.fxml"));
loader.setController(new GUIController(width, height));
final Parent root =
FXMLLoader.load(getClass().getResource("GUI.fxml"));
final Scene scene = new Scene(root);
primaryStage.setTitle("GUI");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
这是我的控制器类:
import javafx.fxml.FXML;
import javafx.scene.layout.GridPane;
public class GUIController {
private int width;
private int height;
public GUIController(int givenWidth, int givenHeight) {//runs this
width = givenWidth;
height = givenHeight;
}
@FXML
public void initialize() { //doesn't run this
SetGrid.build(gridpane, width, height);
}
这是我第一次用 javafx 写东西,所以我可能犯了一些简单的错误,对不起。
【问题讨论】:
-
你确定它没有运行吗?您是否尝试过将
System.out.println(...)作为initialize的第一条语句? -
是的,当我打开它时我可以看到没有添加列(也没有触发我在 setGrid 的构建方法中的打印语句
-
如果你一直坚持静态访问,你很快就会遇到更多麻烦。开始使用对象!