简体   繁体   English

如何在 JavaFX 中切换场景

[英]How to switch scenes in JavaFX

I have looked on many pages to try and find out how to switch scenes but I have been unsuccessful.我已经查看了很多页面以尝试找出如何切换场景,但我没有成功。

I have a calculator and my goal is to select a menu option to change Calculators(ie: basic and scientific).我有一个计算器,我的目标是选择一个菜单选项来更改计算器(即:基本计算器和科学计算器)。 Right now I am just testing so here is my code relevant to this question thus far (I am using Scene Builder):现在我只是在测试,所以这是我到目前为止与这个问题相关的代码(我正在使用 Scene Builder):

@FXML private MenuItem basic;
@FXML private MenuItem testSwitch;


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

}
@Override
public void start(Stage primaryStage) throws Exception
{
   Parent pane = FXMLLoader.load(
           getClass().getResource( "calculator.fxml" ) );

   Scene scene = new Scene( pane );
   primaryStage.setScene(scene);
   primaryStage.setTitle( "Calculator" );
   primaryStage.show();

}
@FXML
public void handleMenuOption(ActionEvent e) 
{
    if(e.getSource()==basic)
    {
        changeScene("calculator.fxml");
    }
    else if(e.getSource()==testSwitch)
    {
        changeScene("TestSwitch.fxml");
    }
}
public void changeScene(String fxml) 
{
    //this prints out
    System.out.println(fxml);
}

EDIT I've tried quite a few things already.编辑我已经尝试了很多东西。 No matter what, I always get this NullPointerException.无论如何,我总是得到这个 NullPointerException。 I have a feeling it may have to do with setting something in scene builder but I just have not been able to find an answer我觉得这可能与在场景生成器中设置某些东西有关,但我一直无法找到答案

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.control.MenuItem.fire(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at    com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
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)
... 44 more
Caused by: java.lang.NullPointerException
at CalculatorMain.changeScene(CalculatorMain.java:75)
at CalculatorMain.handleMenuOption(CalculatorMain.java:64)
... 53 more




at CalculatorMain.changeScene(CalculatorMain.java:75)
This is at:stage . getScene() . setRoot(pane);


at CalculatorMain.handleMenuOption(CalculatorMain.java:64)
This is at:changeScene ("TestSwitch.fxml");

WORKING CODE:工作代码:

I played around using suggestions below and used this code to make it work:我尝试使用下面的建议并使用此代码使其工作:

private Stage stage;

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

@Override
public void start(Stage primaryStage) throws Exception
{
    this.stage = primaryStage;
    FXMLLoader loader = new FXMLLoader(getClass()
            .getResource("calculator.fxml"));
    Parent root = (Parent)loader.load();
    BasicCalculatorView controller = (BasicCalculatorView)loader.getController();
    controller.setModel(new BasicCalculatorModelTest(controller));
    controller.setLogic(this);
    primaryStage.setTitle("Calculator");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

public void switchScene(String fxmlFile)
{

    FXMLLoader loader = new FXMLLoader(getClass()
            .getResource(fxmlFile));
    Parent root;
    try 
    {
        root = (Parent)loader.load();
        if(fxmlFile.equals("calculator.fxml"))
        {
            BasicCalculatorView controller = (BasicCalculatorView)loader.getController();
            controller.setModel(new BasicCalculatorModelTest(controller));
            controller.setLogic(this);
        }
        else if(fxmlFile.equals("TestSwitch.fxml"))
        {
            TestSwitch controller = (TestSwitch)loader.getController();
            controller.setLogic(this);
        }
        this.stage.setScene(new Scene(root));
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }

}

I wrote this controller to keep track of the different scenegraphes.我写了这个控制器来跟踪不同的场景图。

public class ScreenController {
    private HashMap<String, Pane> screenMap = new HashMap<>();
    private Scene main;

    public ScreenController(Scene main) {
        this.main = main;
    }

    protected void addScreen(String name, Pane pane){
         screenMap.put(name, pane);
    }

    protected void removeScreen(String name){
        screenMap.remove(name);
    }

    protected void activate(String name){
        main.setRoot( screenMap.get(name) );
    }
}

So I can write:所以我可以写:

ScreenController screenController = new ScreenController(scene);
screenController.add("calculator", FXMLLoader.load(getClass().getResource( "calculator.fxml" )));
screenController.add("testSwitch", FXMLLoader.load(getClass().getResource( "TestSwitch.fxml" )));
screenController.activate("calculator");

This was a workaround for a fullscreen application, where the MacOS fullscreen transition was shown every time a stage switches its scene.这是全屏应用程序的解决方法,每次舞台切换场景时都会显示 MacOS 全屏过渡。

Instead of switching Scenes , switch a root node on already existing Scene不是切换Scenes ,而是在已经存在的Scene上切换根节点

If you want to go along with changing the scene you would do it like this (note that the Stage is a member of the application):如果您想继续更改场景,您可以这样做(注意舞台是应用程序的成员):

private Stage primaryStage;

@Override
public void start(Stage primaryStage) throws Exception {
    this.primaryStage = primaryStage;
    ...
}

public void changeScene(String fxml){
    Parent pane = FXMLLoader.load(
           getClass().getResource(fxml));

   Scene scene = new Scene( pane );
   primaryStage.setScene(scene);
}

However as already pointed out by @Eugene_Ryzhikov it is a better solution to just change the root content of the existing scene:然而,正如@Eugene_Ryzhikov 已经指出的那样,仅更改现有场景的根内容是一个更好的解决方案:

public void changeScene(String fxml){
    Parent pane = FXMLLoader.load(
           getClass().getResource(fxml));

   primaryStage.getScene().setRoot(pane);
}

I just ran to the same issue and this answer solved my issue perfectly while being short and clean.我刚刚遇到了同样的问题,这个答案既简短又干净,完美地解决了我的问题。

@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
//Here I want to swap the screen!

Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow();
// OR
Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow();
// these two of them return the same stage
// Swap screen
stage.setScene(new Scene(new Pane()));

}

I wrote a library that makes switching Scenes really simple.我写了一个库,使切换场景变得非常简单。 You just pick a unique ID for each scene of type Integer, then you add scenes with one line of code.您只需为每个 Integer 类型的场景选择一个唯一的 ID,然后用一行代码添加场景。 When you need to show a scene, you can do so from any class in your project by calling that method and passing it the the ID of the scene.当您需要显示场景时,您可以从项目中的任何类通过调用该方法并将场景的 ID 传递给它来执行此操作。

You can find the library here along with the Maven import xml.您可以在此处找到该库以及 Maven 导入 xml。

TypesController.java
package todoapp;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.stage.Stage;

public class TypesController implements Initializable {
@FXML
private CheckBox c1;
@FXML
private CheckBox c2;
public void clicked(ActionEvent e) throws IOException {
Parent home_page_parent =FXMLLoader.load(getClass().getResource("AddDcuFXML.fxml"));
Scene home_page_scene = new Scene(home_page_parent);
Stage app_stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
app_stage.hide(); //optional
app_stage.setScene(home_page_scene);
app_stage.show();
}
public void clicked1(ActionEvent e) throws IOException {
Parent home_page_parent =   FXMLLoader.load(getClass().getResource("AddDcuFXML.fxml"));
    Scene home_page_scene = new Scene(home_page_parent);
    Stage app_stage = (Stage) ((Node)e.getSource()).getScene().getWindow();
   app_stage.hide(); //optional
   app_stage.setScene(home_page_scene);
   app_stage.show();
   }
  @Override
  public void initialize(URL arg0, ResourceBundle arg1) {
 // TODO Auto-generated method stub
  } }

The simplest way (This avoids re-loading scene):最简单的方法(这避免了重新加载场景):

In main Application class:在主应用程序类中:

  1. Create a private static attribute to store your stage:创建一个私有静态属性来存储您的舞台:

     private static Stage primaryStage;
  2. Assign stage to your primaryStage in overridden start(Stage stage) method:在覆盖的start(Stage stage)方法中将阶段分配给您的 primaryStage:

     primaryStage = stage;
  3. Create static method changeScene创建静态方法 changeScene

     public static void changeScene(String fxml) throws IOException { Parent pane = FXMLLoader.load(Objects.requireNonNull(GameApplication.class.getResource(fxml))); primaryStage.getScene().setRoot(pane);

This method you can use wherever you want, in any class:您可以在任何类的任何地方使用此方法:

Example:例子:

MainApplication.changeScene("next-scene-view.fxml");
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
 <?import javafx.scene.text.*?>
 <?import java.lang.*?>
 <?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="491.0" prefWidth="386.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="todoapp.TypesController">
<children>
  <CheckBox fx:id="c1" layoutX="55.0" layoutY="125.0" mnemonicParsing="false" onAction="#clicked" text="ADD dcu" />
  <CheckBox fx:id="c2" layoutX="55.0" layoutY="177.0" mnemonicParsing="false" onAction="#clicked1" text="Display dcu" />
  <Label layoutX="31.0" layoutY="58.0" prefHeight="37.0" prefWidth="276.0" text="Choose any one of the options" textFill="#1b29cd">
     <font>
        <Font name="Arial Bold" size="18.0" />
     </font>
  </Label>
</children>

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

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