JavaFXでのページ遷移

前提

java・javaFXの初学者です。
稚拙な質問ですが何卒ご容赦ください。
スクリーンショットの画面をアプリケーションのホーム画面だとします。

実現したいこと

左のAssetsボタンをクリックした時、新しいウィンドウではなくMain Paneと書かれているペイン上に
Assetsに相当するページが開かれるようにするにはどのようにすればよいでしょうか?

スクリーンショット

イメージ説明

ソースコード

HelloController.java

1package com.example.demo12; 2 3import java.net.URL; 4import java.util.ResourceBundle; 5import javafx.event.ActionEvent; 6import javafx.fxml.FXML; 7import javafx.scene.layout.AnchorPane; 8 9public class HelloController { 10 11 @FXML 12 private ResourceBundle resources; 13 14 @FXML 15 private URL location; 16 17 @FXML 18 private AnchorPane mainPane; 19 20 @FXML 21 protected void onClickHome(ActionEvent evt) { 22 System.out.println("Clicked"); 23 } 24 25 @FXML 26 void initialize() { 27 assert mainPane != null : "fx:id=\"mainPane\" was not injected: check your FXML file 'HelloView.fxml'."; 28 29 } 30}

HelloApplication.java

1package com.example.demo12; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5 6import javafx.scene.Scene; 7import javafx.stage.Stage; 8 9import java.io.IOException; 10 11public class HelloApplication extends Application { 12 @Override 13 public void start(Stage stage) throws IOException { 14 FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("HelloView.fxml")); 15 Scene scene = new Scene(fxmlLoader.load(), 320, 240); 16 stage.setTitle("Hello!"); 17 stage.setScene(scene); 18 stage.show(); 19 } 20 21 public static void main(String[] args) { 22 launch(); 23 } 24}

HelloView.fxml

1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.geometry.*?> 4<?import javafx.scene.control.*?> 5<?import javafx.scene.layout.*?> 6<?import javafx.scene.text.*?> 7 8<SplitPane dividerPositions="0.29797979797979796" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.demo12.HelloController"> 9 <items> 10 <AnchorPane maxWidth="-Infinity" minHeight="0.0" minWidth="0.0" prefHeight="300.0" prefWidth="100.0"> 11 <children> 12 <VBox prefHeight="400.0" prefWidth="100.0"> 13 <children> 14 <Button mnemonicParsing="false" onAction="#onClickHome" prefWidth="100.0" text="Home"> 15 <VBox.margin> 16 <Insets top="50.0" /> 17 </VBox.margin> 18 <font> 19 <Font size="14.0" /> 20 </font> 21 </Button> 22 <Button mnemonicParsing="false" prefHeight="35.0" prefWidth="104.0" text="Assets"> 23 <VBox.margin> 24 <Insets top="10.0" /> 25 </VBox.margin> 26 <font> 27 <Font size="14.0" /> 28 </font> 29 </Button> 30 </children> 31 </VBox> 32 </children></AnchorPane> 33 <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"> 34 <children> 35 <SplitPane dividerPositions="0.1407035175879397" orientation="VERTICAL" prefHeight="400.0" prefWidth="500.0"> 36 <items> 37 <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 38 <children> 39 <Label text="Utils"> 40 <font> 41 <Font size="22.0" /> 42 </font> 43 <padding> 44 <Insets left="10.0" top="5.0" /> 45 </padding> 46 </Label> 47 </children> 48 </AnchorPane> 49 <AnchorPane fx:id="mainPane" minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0"> 50 <children> 51 <Label layoutX="113.0" layoutY="104.0" prefHeight="81.0" prefWidth="240.0" text="Main Pane"> 52 <font> 53 <Font size="24.0" /> 54 </font> 55 <padding> 56 <Insets left="100.0" top="150.0" /> 57 </padding> 58 </Label> 59 </children> 60 </AnchorPane> 61 </items> 62 </SplitPane> 63 </children></AnchorPane> 64 </items> 65</SplitPane>

コメントを投稿

0 コメント