JavaFX-在维护工具栏的同时切换视图
我想创建一个具有两个不同视图(因此我可以更改根)的应用程序,但是使用相同的工具栏(单击某些按钮后会更改根)。到目前为止,我尝试了在fxtml文件中硬编码工具栏,以及在我的主应用程序中创建工具栏(但问题是我需要更改不再包含工具栏的场景)。现在,我的代码如下所示:JavaFX-在维护工具栏的同时切换视图,java,javafx,toolbar,Java,Javafx,Toolbar,我想创建一个具有两个不同视图(因此我可以更改根)的应用程序,但是使用相同的工具栏(单击某些按钮后会更改根)。到目前为止,我尝试了在fxtml文件中硬编码工具栏,以及在我的主应用程序中创建工具栏(但问题是我需要更改不再包含工具栏的场景)。现在,我的代码如下所示: package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; impo
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent bikeScene = FXMLLoader.load(getClass().getResource("bikes.fxml"));
Button bikes = new Button("Bikes");
bikes.getStylesheets().add("tool-bar");
bikes.setOnAction(e->primaryStage.setScene(new Scene(bikeScene, 900, 1900)));
ToolBar tb = new ToolBar();
tb.getItems().add(bikes);
primaryStage.setTitle("management app");
VBox vb = new VBox(tb);
Scene sc = new Scene(vb, 900, 1900);
sc.getStylesheets().add("sample/style.css");
primaryStage.setScene(sc);
primaryStage.setMaximized(true);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我知道这很糟糕,但我很困惑,不知道在创建场景时如何合并根和工具栏
编辑:
因此,我决定将mainView作为我的根,现在我有:
public class Main extends Application {
public void start(Stage primaryStage) throws Exception{
Button bikes = new Button("Bikes");
bikes.getStyleClass().add("tool-bar");
Button rooms = new Button("Rooms");
rooms.getStyleClass().add("tool-bar");
Button food = new Button("Food");
food.getStyleClass().add("tool-bar");
Button cars = new Button("Cars");
cars.getStyleClass().add("tool-bar");
Button admin = new Button("Admin");
admin.getStyleClass().add("tool-bar");
BorderPane mainView = new BorderPane();
BorderPane ToolBorderPane = new BorderPane();
ToolBar tBarLeft=new ToolBar();
ToolBar tBarRight=new ToolBar();
tBarLeft.getItems().addAll(rooms, bikes, food, cars);
tBarRight.getItems().add(admin);
ToolBorderPane.setLeft(tBarLeft);
ToolBorderPane.setRight(tBarRight);
ToolBorderPane.getStyleClass().add("tool-bar");
mainView.setTop(ToolBorderPane);
bikes.setOnAction(e-> {
try {
mainView.getChildren().add(loader("bikes.fxml"));
} catch (IOException ex) {
ex.printStackTrace();
}
});
rooms.setOnAction(e-> {
try {
mainView.getChildren().add(loader("rooms.fxml"));
} catch (IOException ex) {
ex.printStackTrace();
}
});
food.setOnAction(e-> {
try {
mainView.getChildren().add(loader("food.fxml"));
} catch (IOException ex) {
ex.printStackTrace();
}
});
cars.setOnAction(e-> {
try {
mainView.getChildren().add(loader("cars.fxml"));
} catch (IOException ex) {
ex.printStackTrace();
}
});
admin.setOnAction(e->
{
try {
mainView.getChildren().add(loader("admin.fxml"));
} catch (IOException ex) {
ex.printStackTrace();
}
});
GridPane loader = loader("sample.fxml");
mainView.getChildren().add(loader);
primaryStage.setTitle("management app");
Scene sc = new Scene(mainView, 1900, 1900);
sc.getStylesheets().add("sample/style.css");
primaryStage.setScene(sc);
primaryStage.setMaximized(true);
primaryStage.show();
}
public GridPane loader(String string) throws IOException {
GridPane loader = FXMLLoader.load(getClass().getResource(string));
return loader;
}
public static void main(String[] args) {
launch(args);
}
}
从理论上讲,它是可以工作的,但在点击某些按钮后,我会得到格式非常奇怪的内容。
例如,my rooms.fxml文件如下所示:
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<Label alignment="BOTTOM_RIGHT" text="let's see if it works" />
</GridPane>
xmlns:fx=”http://javafx.com/fxml“alignment=“center”hgap=“10”vgap=“10”>
文本显示在左上角而不是右下角 不要改变根。让根目录成为一个包含两个子项的面板:工具栏和主视图。更改主视图并保留工具栏。