JavaFX控制器没有获取内容

时间:2017-06-05 15:34:46

标签: java javafx

我是新手在JavaFX中使用控制器而我遇到了麻烦。我的第一个场景加载很好但是当我点击按钮移动到第二个场景时,没有任何东西只出现一个空白框。关于如何加载我的第二个场景的任何线索?

这是我的首发场景:

public class ControllerConnect implements Controller{
    private final FlowPane root;
    public ControllerConnect() {
        //Connection page objects
        TextField username = new TextField();
        TextField password = new TextField();
        username.setAlignment(Pos.CENTER);
        password.setAlignment(Pos.CENTER);
        Button connect = new Button("Connect");
        connect.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent e) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException ex) {
                    System.out.println("Error: unable to load driver class!");
                    System.exit(1);
                }
                String URL = "jdbc:mysql://localhost:3306/world";
                String name = username.getText();
                String pass = password.getText();

                try {
                    Connection conn = DriverManager.getConnection(URL, name, pass);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                ControllerCity controller2 = new ControllerCity();
                connect.getScene().setRoot(controller2.getContent());


            }
        });
        root = new FlowPane(connect);
        Label user = new Label("Username:");
        Label pass = new Label("Password:");
        user.setAlignment(Pos.CENTER);
        pass.setAlignment(Pos.CENTER);
        username.setAlignment(Pos.CENTER);
        password.setAlignment(Pos.CENTER);
        connect.setAlignment(Pos.CENTER);
        VBox connectionBox = new VBox(10, user, username, pass, password, connect);
        connectionBox.setAlignment(Pos.CENTER);
        root.getChildren().addAll(connectionBox);
        root.setMinSize(640, 480);
        root.setAlignment(Pos.CENTER);


    }
    @Override
    public Parent getContent() {
        return root;
    }
}

这是我的第二个场景,只有一半完成,但按钮应显示:

public class ControllerCity implements Controller {
    private final BorderPane root = new BorderPane();
    public ControllerCity() {
        //Layout for city pane
        //HBox for buttons

        Button populate = new Button("Populate/Update");
        Button delete = new Button("Delete");
        Button create = new Button("Create");
        HBox cityButtons = new HBox(populate, delete, create);
        root.setBottom(cityButtons);
        //HBox for TableView
        HBox cityTable = new HBox();
        root.setCenter(cityTable);
        root.setMinSize(1024, 768);

    }
    @Override
    public Parent getContent() {
        return root;
    }
}

申请类:

public void start(Stage primaryStage) throws Exception {
    ControllerConnect controllerconnect = new ControllerConnect();
    Scene scene = new Scene(
            controllerconnect.getContent()
    );

    primaryStage.setScene(scene);
    primaryStage.show();
}

1 个答案:

答案 0 :(得分:2)

按钮存在:您无法看到它们。

问题是你的第一个&#34;控制器&#34; (肯定是视图,而不是控制器)设置640 x 480像素的最小大小。场景将根据首选大小调整,在这种情况下为640 x 480(因为第一个场景中的控件不需要更多空间)。

您的第二个场景设置的最小尺寸为1024 x 768。所以第二个视图现在比包含它的舞台大,并且按钮不在视野范围内。

如果在按下&#34; Connect&#34;之后展开窗口按钮,您将看到按钮。

如果您更换

ControllerCity controller2 = new ControllerCity();
connect.getScene().setRoot(controller2.getContent());
使用

在按钮的事件处理程序中

ControllerCity controller2 = new ControllerCity();
Scene scene = connect.getScene();
scene.setRoot(controller2.getContent());
scene.getWindow().sizeToScene();

然后窗口将调整大小,以便您可以看到按钮。