Scene Builder 8.0&的Intelli。从一个场景到另一个场景

时间:2015-10-02 14:04:48

标签: java javafx javafx-2 javafx-8 scenebuilder

如何使用Scene Builder中的buttons / menuItem将两个场景相互连接?

我做了一个新的测试 - 项目并尝试了Hips向我展示的内容,但是我遇到了一些错误。这并不是Hips所做的,所以这就是为什么我会得到错误而不是他: - )

主要课程。

package sample;

public class Main extends Application {

Stage window;
Scene mainScene, eventScene;
BorderPane mainLayout;
StackPane eventLayout;

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;

    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("main.fxml"));
    mainLayout = loader.load();
    MainController mainController = loader.getController();

    loader = new FXMLLoader();
    loader.setLocation(Main.class.getResource("event.fxml"));
    eventLayout = loader.load();
    EventController eventController = loader.getController();

    mainScene = new Scene(mainLayout);
    eventScene = new Scene(eventLayout);


    window.setScene(mainScene);
    window.setTitle("Test Project");
    window.show();

}

public static void main(String[] args) {
    launch(args);
}

public void setScene(Scene scene) {
    window.setScene(scene);
}
}

MainController。

package sample;


public class MainController {

private Scene eventScene;
private Main main;

public void setMain(Main main){
    this.main = main;
}
public void seteventScene(Scene eventScene) {
    this.eventScene = eventScene;
}

@FXML
public void switchScene() {
    main.setScene(eventScene);

}
}

main.FXML

<?xml version="1.0" encoding="UTF-8"?>

<BorderPane fx:id="mainLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
<top>
  <VBox fx:id="vbox" BorderPane.alignment="CENTER">
     <children>
        <MenuBar fx:id="menuBar">
          <menus>
            <Menu fx:id="fileMenu" mnemonicParsing="false" text="File">
              <items>
                <MenuItem fx:id="newItem" mnemonicParsing="false" onAction="#switchScene" text="New..." />
                    <MenuItem fx:id="openItem" mnemonicParsing="false" text="Open..." />
                    <MenuItem fx:id="saveItem" mnemonicParsing="false" text="Save..." />
                    <SeparatorMenuItem mnemonicParsing="false" />
                    <MenuItem fx:id="exitItem" mnemonicParsing="false" text="Exit" />
              </items>
            </Menu>
            <Menu fx:id="editMenu" mnemonicParsing="false" text="Edit">
              <items>
                <MenuItem mnemonicParsing="false" text="Delete" />
              </items>
            </Menu>
            <Menu fx:id="helpMenu" mnemonicParsing="false" text="Help">
              <items>
                <MenuItem mnemonicParsing="false" text="About" />
              </items>
            </Menu>
          </menus>
        </MenuBar>
     </children>
  </VBox>
</top>
</BorderPane>

EventController。

package sample;


public class EventController {

private Scene mainScene;
private Main main;

public void setMain(Main main) {
    this.main = main;
}

public void setmainScene(Scene mainScene) {
    this.mainScene = mainScene;

}

@FXML
public void goBack() {
    main.setScene(mainScene);

}
}

event.FXML。

<?xml version="1.0" encoding="UTF-8"?>


<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="550.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.EventController" />

当我点击'New ...'MenuItem时,这就是我得到的错误。

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
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.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$MouseHandler.process(Scene.java:3757)
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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.    java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 43 more
Caused by: java.lang.NullPointerException
at sample.MainController.switchScene(MainController.java:21)
... 53 more

1 个答案:

答案 0 :(得分:1)

这是我认为是如何实现这样的事情的最小例子(在IntelliJ;中) - 如果您有任何特定的问题,可以随意询问。

步骤:

  • 创建新的Java项目
  • 创建两个控制器文件
  • 创建两个emtpy文件,并将它们命名为scene1.fxml和scene2.fxml
  • 使用SceneBuilder和。使用一些元素填充.fxml文件 将参考传递给各自的控制人员。 (在我的情况下都是 场景只包含一个窗格和一个onAction方法的按钮 用来切换场景。
  • 使用以下代码:

Main.java

package sample;

import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;

import java.io.IOException;

public class Main extends Application {

    // Primary Stage
    Stage window;
    // Two scenes
    Scene scene1, scene2;
    // The panes are associated with the respective .fxml files
    private Pane pane1;
    private Pane pane2;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            // Set the window as primary stage
            window = primaryStage;

            // Load the fxml files and their controllers
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("scene1.fxml"));
            pane1 = loader.load();
            Scene1Controller controller1 = loader.getController();

            loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("scene2.fxml"));
            pane2 = loader.load();
            Scene2Controller controller2 = loader.getController();

            // The scenes are based on what has been loaded from the .fxml files
            Scene scene1 = new Scene(pane1);
            Scene scene2 = new Scene(pane2);

            // Pass reference the each scenes controller
            controller1.setScene2(scene2);
            controller1.setMain(this);
            controller2.setScene1(scene1);
            controller2.setMain(this);

            //Display scene 1 at first
            window.setScene(scene1);
            window.setTitle("Scene!");
            window.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // used by the controllers to switch the scenes
    public void setScene(Scene scene){
        window.setScene(scene);
    }
}

Scene1Controller.java

package sample;

import javafx.scene.Scene;
import javafx.stage.Window;

public class Scene1Controller {

    private Scene scene2;
    private Main main;

    public void setMain(Main main){
        this.main = main;
    }
    public void setScene2(Scene scene2){
        this.scene2 = scene2;
    }
    // this method is called by clicking the button
    @FXML
    public void switchScene(){main.setScene(scene2);}
}

Scene2Controller.java

package sample;

import javafx.scene.Scene;
import javafx.stage.Window;

public class Scene2Controller {

    private Scene scene1;
    private Main main;

    public void setMain(Main main){this.main = main;}
    public void setScene1(Scene scene1){this.scene1 = scene1;}
    // this method is called by clicking the button
    @FXML
    public void goBack(){main.setScene(scene1);}
}

scene1.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Scene1Controller">
   <children>
      <Button layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#switchScene" text="Go to scene 2!" />
   </children>
</Pane>

scene2.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Scene2Controller">
   <children>
      <Button layoutX="194.0" layoutY="175.0" mnemonicParsing="false" onAction="#goBack" text="Go back!" />
   </children>
</Pane>