JavaFX-加载下一个场景时保留工具栏
加载新场景时如何保留工具栏 这是我目前的代码: Main.classJavaFX-加载下一个场景时保留工具栏,java,javafx,Java,Javafx,加载新场景时如何保留工具栏 这是我目前的代码: Main.class public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); primaryStage
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class Main extends Application {
private static Stage pStage;
// Creating a static root to pass to the controller
private static BorderPane root = new BorderPane();
@Override
public void start(Stage primaryStage) throws Exception {
Parent main = FXMLLoader.load(getClass().getResource("Main.fxml"));
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
root.setTop(main);
root.setCenter(screen1);
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
/**
* Just a root getter for the controller to use
*/
public static BorderPane getRoot() {
return root;
}
public class Controller {
@FXML
private void testButton1() throws IOException {
try {
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen1);
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void testButton2() throws IOException {
try {
AnchorPane screen2 = FXMLLoader.load(getClass().getResource("screen2.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen2);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Main.fxml
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<GridPane fx:controller="main.java.Controller"
xmlns:fx="http://javafx.com/fxml">
<ToolBar prefHeight="40.0" prefWidth="400.0">
<Button text="testButton" onAction="#testButton"/>
</ToolBar>
</GridPane>
对于替换部分内容,有比
GridPane
更合适的布局,但基本上您需要为工具栏
使用父级,以便替换其他子级。在这种情况下,BorderPane
可能是个好主意,但也可以使用GridPane
来实现;只是不那么优雅
如果您继续使用网格窗格
,使用fx:id
注入它,确保工具栏
是您要保留的唯一子项,它是第一个子项,也是位于行索引=0,列索引=0)的唯一子项
,您还可以手动删除其他子项:
@FXML
private GridPane container;
@FXML
private void testButton1() throws IOException {
ObservableList<Node> children = container.getChildren();
children.remove(1, children.size()); // remove every child after the first
try {
container.add(FXMLLoader.load(getClass().getResource("src/testButton1.fxml")), 0, 1);
container.getScene().getWindow().sizeToScene(); // resize scene to fit the full size of the content
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
专用网格窗格容器;
@FXML
私有void testButton1()引发IOException{
ObservableList children=container.getChildren();
children.remove(1,children.size());//删除第一个
试一试{
add(fxmloader.load(getClass().getResource(“src/testButton1.fxml”)),0,1);
container.getScene().getWindow().sizeToScene();//调整场景大小以适应内容的完整大小
}捕获(IOE异常){
e、 printStackTrace();
}
}
对于包含工具栏的主阶段,您需要有一个单独的FXML
,然后使用边框窗格
可以在工具栏和其他屏幕之间进行分隔
参见下面的示例
Main.class
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class Main extends Application {
private static Stage pStage;
// Creating a static root to pass to the controller
private static BorderPane root = new BorderPane();
@Override
public void start(Stage primaryStage) throws Exception {
Parent main = FXMLLoader.load(getClass().getResource("Main.fxml"));
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
root.setTop(main);
root.setCenter(screen1);
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
/**
* Just a root getter for the controller to use
*/
public static BorderPane getRoot() {
return root;
}
public class Controller {
@FXML
private void testButton1() throws IOException {
try {
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen1);
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void testButton2() throws IOException {
try {
AnchorPane screen2 = FXMLLoader.load(getClass().getResource("screen2.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen2);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
控制器类
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
public class Main extends Application {
private static Stage pStage;
// Creating a static root to pass to the controller
private static BorderPane root = new BorderPane();
@Override
public void start(Stage primaryStage) throws Exception {
Parent main = FXMLLoader.load(getClass().getResource("Main.fxml"));
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
root.setTop(main);
root.setCenter(screen1);
primaryStage.setTitle("Test FXML application");
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
/**
* Just a root getter for the controller to use
*/
public static BorderPane getRoot() {
return root;
}
public class Controller {
@FXML
private void testButton1() throws IOException {
try {
AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen1);
}
catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void testButton2() throws IOException {
try {
AnchorPane screen2 = FXMLLoader.load(getClass().getResource("screen2.fxml"));
BorderPane border = Main.getRoot();
border.setCenter(screen2);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Main.fxml(由场景生成器生成)
场景1
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="10.0" layoutY="10.0" prefHeight="36.0" prefWidth="374.0" text="SCREEN 1">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>