JavaFX导航栏和内容窗格
我想在我的新项目中使用JavaFX,并希望在下面的屏幕截图中看到类似的内容 在左边的网站我需要一个导航栏,在右边我的内容。因此,我会在左侧使用VBox,在右侧使用AnchorPane(或者更好的滚动窗格) 当我点击“安全”按钮时,应该会在右侧加载我的“安全”场景。但我怎么能做到这一点呢。没有找到任何解决方案JavaFX导航栏和内容窗格,java,javafx,navigationbar,contentpane,Java,Javafx,Navigationbar,Contentpane,我想在我的新项目中使用JavaFX,并希望在下面的屏幕截图中看到类似的内容 在左边的网站我需要一个导航栏,在右边我的内容。因此,我会在左侧使用VBox,在右侧使用AnchorPane(或者更好的滚动窗格) 当我点击“安全”按钮时,应该会在右侧加载我的“安全”场景。但我怎么能做到这一点呢。没有找到任何解决方案 非常感谢这是此类导航的典型实现。此处默认加载view_1.fxml中描述的视图: <BorderPane fx:id="mainBorderPane" fx:controller="
非常感谢这是此类导航的典型实现。此处默认加载
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>
更新
这是一种转换,其中视图直接列在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>
你试着为自己编写什么代码?
<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();
}
}
}