简体   繁体   English

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

[英]Scene Builder 8.0 & Intelli. From one scene to another scene

How do I connect two scenes to each other with buttons/menuItems in Scene Builder? 如何在Scene Builder中使用按钮/菜单项将两个场景相互连接?

I have done a new test-Project and tried what Hips showed me, but I get some errors. 我已经完成了一个新的测试项目,并尝试了Hips向我展示的内容,但出现了一些错误。 It's not exactly what Hips did so thats why I get errors but not him :-) 臀部并不是完全这么做的,这就是为什么我得到错误而不是他的原因:-)

Main class. 主班。

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. 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 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. 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. 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" />

WHEN I click on the 'New...' MenuItem, this is the error I get. 当我单击“新建...”菜单项时,这是我收到的错误。

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

This is what I would consider to be a minimal example of how something like this can be achieved (in IntelliJ ;) ) - if you have any specific problems with IntelliJ feel free to ask. 如果您有任何与IntelliJ有关的特定问题,我将认为这是如何实现这样的最小示例(在IntelliJ中;)。

Steps: 脚步:

  • Create a new Java Project 创建一个新的Java项目
  • Create two Controller Files 创建两个控制器文件
  • Create two emtpy files and name them scene1.fxml and scene2.fxml 创建两个空文件,并将它们命名为scene1.fxml和scene2.fxml
  • Fill both .fxml files with some elements using SceneBuilder and pass reference to their respective Controller. 使用SceneBuilder使用某些元素填充两个.fxml文件,并将引用传递给它们各自的Controller。 (in my case both scenes only consist ofa pane and a Button which onAction method is used to switch the scenes. (在我的情况下,两个场景仅由一个窗格和一个Button组成,而onAction方法用于切换场景。
  • Use the following code: 使用以下代码:

Main.java 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 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 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 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM