2017-12-29 12 views
0

新しいプロジェクトでJavaFXを使用したいと考えていますが、以下のスクリーンショットのようなものが必要です。JavaFXナビゲーションバーとコンテンツペイン

左側のサイトにはナビゲーションバーが必要ですが、右側にはコンテンツがあります。だから、私は左側のVBoxを使用し、右側のAnchorPane(または、より良いScrollPane)を使用します。

「セキュリティ」ボタンをクリックすると、右側に「セキュリティ」シーンが読み込まれます。しかし、どうすればそれを管理することができますか?これに対する解決策は見つかりませんでした。

enter image description here

どうもありがとう

+1

あなた自身でコードを作成しようとしましたか? – Kerry

答えて

1

これは、このようなナビゲーションの典型的な実装です。ここでview_1.fxmlに記載のビューがデフォルトでロードされる:

<BorderPane fx:id="mainBorderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml"> 
    <left> 
     <VBox spacing="5"> 
      <Button text="btn 1" onAction="#handleShowView1"/> 
      <Button text="btn 2" onAction="#handleShowView2"/> 
      <Button text="btn 3" onAction="#handleShowView3"/> 
     </VBox> 
    </left> 
    <center> 
     <fx:include source="view_1.fxml"/> 
    </center> 
</BorderPane> 

、これはこれはビューが直接、リストされている変換コントローラ

public class Controller { 

    @FXML 
    private BorderPane mainBorderPane; 

    @FXML 
    private void handleShowView1(ActionEvent e) { 
     loadFXML(getClass().getResource("/sample/view_1.fxml")); 
    } 

    @FXML 
    private void handleShowView2(ActionEvent e) { 
     loadFXML(getClass().getResource("/sample/view_2.fxml")); 
    } 

    @FXML 
    private void handleShowView3(ActionEvent e) { 
     loadFXML(getClass().getResource("/sample/view_3.fxml")); 
    } 

    private void loadFXML(URL url) { 
     try { 
      FXMLLoader loader = new FXMLLoader(url); 
      mainBorderPane.setCenter(loader.load()); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

更新

になっていますFXMLファイル

<BorderPane fx:id="mainBorderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml"> 
    <left> 
     <VBox spacing="5"> 
      <Button text="btn 1" userData="/sample/view_1.fxml" onAction="#handleShowView"/> 
      <Button text="btn 2" userData="/sample/view_2.fxml" onAction="#handleShowView"/> 
      <Button text="btn 3" userData="/sample/view_3.fxml" onAction="#handleShowView"/> 
     </VBox> 
    </left> 
    <center> 
     <fx:include source="view_1.fxml"/> 
    </center> 
</BorderPane> 

とコントローラ

public class Controller { 

    @FXML 
    private BorderPane mainBorderPane; 

    @FXML 
    private void handleShowView(ActionEvent e) { 
     String view = (String) ((Node)e.getSource()).getUserData(); 
     loadFXML(getClass().getResource(view)); 
    } 

    private void loadFXML(URL url) { 
     try { 
      FXMLLoader loader = new FXMLLoader(url); 
      mainBorderPane.setCenter(loader.load()); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
関連する問題