gpt4 book ai didi

java - 如何使用fxml文件和流库获取 Controller 中的参数?

转载 作者:行者123 更新时间:2023-11-29 04:39:35 24 4
gpt4 key购买 nike

如何在 MainController 中实例化和初始化 Settings 的构造函数中使用参数“Settings”对象调用 MoviesSettingsController?我必须先在 SideMenuController 上传递设置,然后再在 MoviesSettingsController 上传递?我对此有点迷茫。你会在

之后找到我所有的文件

此刻,我得到这个错误:

io.datafx.controller.flow.FlowException: io.datafx.controller.FxmlLoadException: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
at io.datafx.controller.flow.action.FlowLink.handle(FlowLink.java:72)
at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:231)
at io.datafx.controller.flow.FlowHandler.handle(FlowHandler.java:163)
at maxiRename.controller.SideMenuController.lambda$0(SideMenuController.java:79)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at com.jfoenix.controls.JFXListCell.lambda$updateItem$157(JFXListCell.java:295)
at com.jfoenix.controls.JFXListCell.access$lambda$9(JFXListCell.java)
at com.jfoenix.controls.JFXListCell$$Lambda$10.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: io.datafx.controller.FxmlLoadException: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:180)
at io.datafx.controller.flow.action.FlowLink.handle(FlowLink.java:69)
... 85 more
Caused by: java.lang.InstantiationException: maxiRename.controller.MoviesSettingsController
at java.lang.Class.newInstance(Class.java:427)
at io.datafx.controller.ViewFactory.createByController(ViewFactory.java:148)
... 86 more
Caused by: java.lang.NoSuchMethodException: maxiRename.controller.MoviesSettingsController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 87 more

主应用程序.java:

public void start(Stage stage) throws Exception {
Flow flow = new Flow(MainController.class);
DefaultFlowContainer container = new DefaultFlowContainer();
flowContext = new ViewFlowContext();
flowContext.register("Stage", stage);
flow.createHandler(flowContext).start(container);

JFXDecorator decorator = new JFXDecorator(stage, container.getView());
decorator.setCustomMaximize(true);
Scene scene = new Scene(decorator, 770, 690);
scene.getStylesheets().add(MainMaxiFilm.class.getResource("/resources/css/jfoenix-main-demo.css").toExternalForm());
stage.setWidth(770);
stage.setHeight(690);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}

主 Controller .java:

@FXMLController(value = "/resources/fxml/Main.fxml", title = "Material Design Example")
public class MainController implements Initializable{

@FXMLViewFlowContext
private ViewFlowContext context;
@FXML
private StackPane root;

@FXML
private BorderPane mainBorderPane;

@FXML
private VBox mainVBoxTop;

@FXML
private JFXToolbar mainToolbarTop;

@FXML
private JFXHamburger mainHamburgerTop;

@FXML
private JFXDrawer mainDrawer;

private FlowHandler flowHandler;
private FlowHandler sideMenuFlowHandler;

public void initialize(URL url, ResourceBundle rb) {
try {
HamburgerBackArrowBasicTransition transition = new HamburgerBackArrowBasicTransition(mainHamburgerTop);
transition.setRate(-1);

mainDrawer.setOnDrawerOpening((e) -> {
transition.setRate(1);
transition.play();
});

mainDrawer.setOnDrawerClosing((e) -> {
transition.setRate(-1);
transition.play();
});

mainHamburgerTop.setOnMouseClicked((e)->{
if (mainDrawer. isShown() || mainDrawer.isShowing()) {
mainDrawer.close();
}
else
mainDrawer.open();
});

// create the inner flow and content
context = new ViewFlowContext();
// set the default controller
Flow innerFlow = new Flow(HomeController.class);

flowHandler = innerFlow.createHandler(context);
context.register("ContentFlowHandler", flowHandler);
context.register("ContentFlow", innerFlow);
mainDrawer.setContent(flowHandler.start());
context.register("ContentPane", mainDrawer.getContent().get(0));

// side controller will add links to the content flow
Flow sideMenuFlow = new Flow(SideMenuController.class);
sideMenuFlowHandler = sideMenuFlow.createHandler(context);
mainDrawer.setSidePane(sideMenuFlowHandler.start());
} catch (FlowException ex) {
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

SideMenuController.java:

@FXMLController(value = "/resources/fxml/SideMenu.fxml", title = "Material Design Example")
public class SideMenuController {


@FXML
private StackPane root;

@FXML
private VBox listViewDrawer;

@FXML
private ImageView imageTopDrawer;

@FXML
private JFXListView<?> sideList;

@FXML
private Label home;

@FXML
private JFXListView<?> subList;

@FXML
private Label musics;

@FXML
private Label movies;

@FXML
private Label series;

@FXML
private Label help;

@FXML
private Label exit;

@FXMLViewFlowContext
private ViewFlowContext context;

@PostConstruct
public void init() throws FlowException, VetoException {
sideList.propagateMouseEventsToParent();
System.out.println(context);
FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
System.out.println(subList.getItems().get(0));
bindNodeToController(home, HomeController.class, contentFlow, contentFlowHandler);
bindNodeToController(musics, MusicsSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(movies, MoviesSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(series, SeriesSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(help, HelpController.class, contentFlow, contentFlowHandler);
}

private void bindNodeToController(Node node, Class<?> controllerClass, Flow flow, FlowHandler flowHandler) {
flow.withGlobalLink(node.getId(), controllerClass);
node.setOnMouseClicked((e) -> {
try {
flowHandler.handle(node.getId());
} catch (Exception e1) {
e1.printStackTrace();
}
});
}
}

和 MoviesSettingsControler.java:

@FXMLController(value = "/resources/fxml/MoviesSettings.fxml" , title = "Material Design Example")
public class MoviesSettingsController{

@FXML
private GridPane gridSettingsMovies;

@FXML
private Label labelSettingsMovies;

@FXML
private Label labelFilesNameSettingsMovies;

@FXML
private JFXButton buttonCancelMoviesSettings;

@FXML
private JFXButton buttonOKMoviesSettings;

@FXML
private GridPane gridFilesName;

@FXML
private ToggleGroup groupFilesName;

@FXML
private JFXRadioButton radioButtonFiles5;

@FXML
private JFXRadioButton radioButtonFiles6;

@FXML
private JFXRadioButton radioButtonFiles3;

@FXML
private JFXRadioButton radioButtonFiles2;

@FXML
private JFXRadioButton radioButtonFiles1;

@FXML
private JFXRadioButton radioButtonFiles4;

private Settings settings = new Settings();

public MoviesSettingsController (Settings pSettings)
{
this.settings = pSettings;
}

public Settings getSettings() {
return settings;
}

public void setSettings(Settings settings) {
this.settings = settings;
}

public void saveChange (int pRb, int pPart)
{
this.settings.setMovieName(pRb);
}

@FXML
void saveSettingsMovies(ActionEvent event) {
System.out.println(groupFilesName.getSelectedToggle());
}

我想要 Controller 中的设置对象。问题是围绕这个(我认为)(代码来自 SideMenuController.java):

@PostConstruct
public void init() throws FlowException, VetoException {
sideList.propagateMouseEventsToParent();
System.out.println(context);
FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");
System.out.println(subList.getItems().get(0));
bindNodeToController(home, HomeController.class, contentFlow, contentFlowHandler);
bindNodeToController(musics, MusicsSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(movies, MoviesSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(series, SeriesSettingsController.class, contentFlow, contentFlowHandler);
bindNodeToController(help, HelpController.class, contentFlow, contentFlowHandler);
}

private void bindNodeToController(Node node, Class<?> controllerClass, Flow flow, FlowHandler flowHandler) {
flow.withGlobalLink(node.getId(), controllerClass);
node.setOnMouseClicked((e) -> {
try {
flowHandler.handle(node.getId());
} catch (Exception e1) {
e1.printStackTrace();
}
});
}

非常感谢:)

最佳答案

好的,我找到了一些有用的东西。如果您看到更好的方法,请告诉我:)。

我在 MainController 中创建了一个这样的上下文:

ViewFlowContext context = new ViewFlowContext();

我用我想通过 Flow 对象传递给其他组件的所有参数和内容填充它。我是这样填的:

@FXML
private JFXDrawer mainDrawer;
private FlowHandler flowHandler;
private FlowHandler sideMenuFlowHandler;

context.register("ContentFlowHandler", flowHandler);
context.register("Settings", new Settings());
context.register("ContentFlow", innerFlow);

我正在使用 Flow 库。我将包含我想要的所有变量和对象的上下文传递给流:

Flow sideMenuFlow = new Flow(SideMenuController.class);
sideMenuFlowHandler = sideMenuFlow.createHandler(context);
mainDrawer.setSidePane(sideMenuFlowHandler.start());

之后进入 sideMenuController。

然后像这样从 context 中取回所有这些东西:

FlowHandler contentFlowHandler = (FlowHandler) context.getRegisteredObject("ContentFlowHandler");
Flow contentFlow = (Flow) context.getRegisteredObject("ContentFlow");

最后,在 movieSettingsController 中,我通过上下文取回值,仅此而已:)

settings = (Settings) context.getRegisteredObject("settings");

最后,不要忘记在您的 Controller 中使用注解 @PostConstruct。示例:

@PostConstruct
public void init() {
// TODO Auto-generated method stub
settings = (Settings) context.getRegisteredObject("Settings");
}

我希望这对其他人有用。当您开始探索这个世界时,事情就没那么容易了。

关于java - 如何使用fxml文件和流库获取 Controller 中的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com