我是初学者程序员,最近我学会了如何在JavaFX中切换场景。我想添加一个边框窗格(作为这些场景的容器)和一些菜单按钮,并在中心设置场景(并且仍然可以在它们之间切换)。我该怎么做或想到什么? 这是我的代码:
public class Main extends Application {
public static final String ROOT = "Root";
public static final String ROOT_FXML = "Root.fxml";
public static final String FIRST_SCREEN = "FirstScreen";
public static final String FIRST_SCREEN_FXML = "FirstScreen.fxml";
public static final String SECOND_SCREEN = "SecondScreen";
public static final String SECOND_SCREEN_FXML = "SecondScreen.fxml";
@Override
public void start(Stage primaryStage) {
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(Main.FIRST_SCREEN, Main.FIRST_SCREEN_FXML);
mainContainer.loadScreen(Main.SECOND_SCREEN, Main.SECOND_SCREEN_FXML);
mainContainer.setScreen(Main.FIRST_SCREEN);
Group root = new Group();
root.getChildren().addAll(mainContainer);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
接口类
public interface ControlledScreen {
public void setScreenParent(ScreensController screenPage);
}
屏幕控制器类
public class ScreensController extends StackPane {
private HashMap<String, Node> screens = new HashMap<>();
public void addScreen(String name, Node screen) {
screens.put(name, screen);
System.out.println("added screen " + name);
}
public boolean loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler = ((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
public boolean setScreen(final String name) {
if (screens.get(name) != null) {
if (!getChildren().isEmpty()) {
getChildren().remove(0);
// add new screen
getChildren().add(0, screens.get(name));
} else {
getChildren().add(screens.get(name));
}
return true;
} else {
System.out.println("screen hasn't been loaded!\n");
return false;
}
}
public boolean unloadScreen(String name) {
if (screens.remove(name) == null) {
System.out.println("Screen didn't exist");
return false;
} else {
return true;
}
}
}
目前我有两个带有两个diffrend fxml文件的控制器类。它们看起来像这样:
public class FirstScreenController implements ControlledScreen {
ScreensController myController;
@FXML
void initialize() {
}
@Override
public void setScreenParent(ScreensController screenPage) {
myController = screenPage;
}
}
有没有办法修复我的代码?
答案 0 :(得分:0)
我解决了问题。这是新的主要课程
public class Main extends Application {
public static final String ROOT = "Root";
public static final String ROOT_FXML = "Root.fxml";
public static final String FIRST_SCREEN = "FirstScreen";
public static final String FIRST_SCREEN_FXML = "FirstScreen.fxml";
public static final String SECOND_SCREEN = "SecondScreen";
public static final String SECOND_SCREEN_FXML = "SecondScreen.fxml";
private Stage primaryStage;
private BorderPane rootLayout;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
ScreensController mainContainer = new ScreensController();
initRootLayout();
mainContainer.loadScreen(Main.FIRST_SCREEN, Main.FIRST_SCREEN_FXML);
mainContainer.loadScreen(Main.SECOND_SCREEN, Main.SECOND_SCREEN_FXML);
mainContainer.setScreen(Main.FIRST_SCREEN);
Group root = new Group();
root.getChildren().addAll(mainContainer);
rootLayout.setCenter(root);
}
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/Root.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}