Im在JavaFX中使用控制器和im有麻煩的新手。我的第一個場景加載正常,但是當我點擊按鈕移動到第二個場景時,沒有任何東西出現,只是一個空白框。任何關於如何加載我的第二場景的線索?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();
}
@Yahya爲什麼不呢? –
我刪除了決賽,但沒有解決問題。 –
這些看起來比控制器更像是視圖。你能告訴應用程序類 - 我以爲你只是創建一個場景,設置它的根是'新ControllerConnect()的getContent()',等有沒有例外,想必? –