源文章(国内不知道要不要用梯子)
MainApp.java
Loads the MainView.fxml
MainView.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns:fx ="http://javafx.com/fxml" >
<center >
<Pane fx:id ="content" />
<fx:include source ="Content1.fxml" />
</center >
<bottom >
<fx:include source ="TaskBar.fxml" />
</bottom >
</BorderPane >
Content1.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx ="http://javafx.com/fxml" fx:id ="content1" >
<Label text ="Hallo Java FX 2.1.1 Content1.fxml" />
</StackPane >
Content2.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane xmlns:fx ="http://javafx.com/fxml" fx:id ="content2" >
<Label text ="Hallo Java FX 2.1.1 Content2.fxml" />
</StackPane >
TaskBar.fxml
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<HBox xmlns:fx ="http://javafx.com/fxml" spacing ="10" alignment ="center"
fx:id ="taskBar" fx:controller ="TaskBarController" >
<children >
<Button fx:id ="taskBarButton1" onAction ="#handleTaskBarButton1Action" />
<Button fx:id ="taskBarButton2" onAction ="#handleTaskBarButton2Action" />
</children >
</HBox >
TaskBarController.java
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
public class TaskBarController implements Initializable {
@FXML
private Button taskBarButton1;
@FXML
private Button taskBarButton2;
@FXML
private void handleTaskBarButton1Action (ActionEvent event) {
System.out.println("click! taskBarButton1" );
}
@FXML
private void handleTaskBarButton2Action (ActionEvent event) {
System.out.println("click! taskBarButton2" );
content.getChildren().clear();
content.getChildren().add(FXMLLoader.load(getClass().getResource("Content2.fxml" ));
}
}
@Override
public void initialize (URL location, ResourceBundle resources) {
}
}
Locate fxml file in other package
example
Parent root = FXMLLoader.load (getClass().getResource("/jfxtest2/Screen.fxml" ));