2017-06-05 58 views
0

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(); 
} 
+2

@Yahya爲什麼不呢? –

+1

我刪除了決賽,但沒有解決問題。 –

+0

這些看起來比控制器更像是視圖。你能告訴應用程序類 - 我以爲你只是創建一個場景,設置它的根是'新ControllerConnect()的getContent()',等有沒有例外,想必? –

回答

2

按鈕在場:你只是看不到他們。

的問題是,你的第一個「控制器」(這肯定是一個視圖,而不是一個控制器)設置的640 x 480像素的最小尺寸。該場景將根據首選大小的根,在這種情況下將爲640 x 480(因爲第一個場景中的控件不需要更多的空間)。

您的第二個場景的最小尺寸爲1024 x 768。所以第二種觀點現在比包含它的舞臺更大,並且按鈕不可見。

如果您按下「連接」按鈕後,展開窗口,你會看到的按鈕。

如果在按鈕的事件處理與

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

更換

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

那麼窗口會調整,所以你可以看到按鈕。

+0

感謝您的仔細觀察。 –