如何从JavaFX中的弹出对话框处理按钮操作?(在eclipse中找不到源代码)

时间:2017-11-18 01:03:37

标签: java javafx fxml

我有两个控制器:mainController(mC)和MainMenuController(MMC)。每个都有自己的FXML文件。我在mainController中有一个按钮,当它被点击时,它会调出MMC。在MMC中,有一个按钮,我想添加一个点击事件。我通过SceneBuilder为此事件分配了适当的方法,但该方法永远不会触发。控制台中没有打印任何内容,应用程序也不会退出。我只是在eclipse中弹出一个“Source not found”对话框,但它没有说明什么来源或什么代码导致错误。我假设MainMenu.fxml找不到它的控制器。我做错了什么?

mainController.java:

public class mainController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;
    @FXML private Button mainMenuBtn;
    @FXML private Stage stage;

    private Stage dialog; //Main menu


    @FXML
    void initialize() {

    }

    @FXML
    private void onMenuClicked(){
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainMenu.fxml"));
            Parent root = (Parent) fxmlLoader.load();
            Stage stage = new Stage();
            stage.setTitle("Main Menu");
            stage.setScene(new Scene(root));
            stage.show();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

mainController.fxml:

<Button fx:id="mainMenuBtn" mnemonicParsing="false" onMouseClicked="#onMenuClicked" prefHeight="32.0" styleClass="icons" text="\\uf013">
     <HBox.margin>
          <Insets right="24.0" top="16.0" />
      </HBox.margin>
</Button>

MainMenuController.java:

public class MainMenuController {
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button btnOptions;

    @FXML
    private Button btnQuit;


    @FXML
    void initialize() {


    }


    @FXML
    void btnQuitClicked(ActionEvent ev){
        System.out.println("quit clicked");
        Platform.exit();
    }
}

MainMenu.fxml:

<Pane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.MainMenuController">
  <!-- TODO Add Nodes -->
  <children>
    <VBox fillWidth="true" layoutX="0.0" layoutY="0.0" prefHeight="200.0" prefWidth="300.0">
      <children>
        <Label alignment="CENTER" prefWidth="300.0" text="Main Menu" textAlignment="CENTER">
          <VBox.margin>
            <Insets top="12.0" fx:id="x1" />
          </VBox.margin>
        </Label>
        <Button id="options" fx:id="btnOptions" mnemonicParsing="false" prefHeight="0.0" prefWidth="300.0" text="Options" textAlignment="CENTER" VBox.margin="$x1" />
        <Button id="quit" fx:id="btnQuit" mnemonicParsing="false" onMouseClicked="#btnQuitClicked" prefHeight="0.0" prefWidth="300.0" text="Quit" textAlignment="CENTER" />
      </children>
    </VBox>
  </children>
</Pane>

如果我在应用程序停止找到Source not found错误后在eclipse中单击“继续”,则应用程序会抛出以下内容:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    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.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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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(Unknown Source)

0 个答案:

没有答案