繁体   English   中英

在两个锚定窗格之间切换

[英]switching between two Anchor Panes

我是JavaFX的新手,我需要在没有菜单栏的两个Anchor Panel之间切换,但是在anchorpane本身的注册按钮,它应该切换到注册表格形式登录表单我使用Scene Builder来生成.FXML文件,

我的问题是如何以简单的方式做到这一点? loginForm与注册按钮

在此输入图像描述

ERRRO是

线程“JavaFX Application Thread”中的异常java.lang.RuntimeException:javafx.fxml.FXMLLoader中的java.lang.reflect.InvocationTargetException $ Javafx.fxml.FXMLLoader $ ControllerHandler.invoke(FXMLLoader.java:1774)$ ControllerMethodEventHandler.handle(FXMLLoader) .java:1657)com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)位于com.sun.javafx的com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)。 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.dispatchEven t(eventDispatchChainImpl.java:114)com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)at com.sun位于javafx.event.Event.fireEvent(Event.java:198)的com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)中的.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)at javafx.scene.Node.fireEvent(Node.java:8413)at javafx.scene.control.Button.fire(Button.java:185)at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior。 java:182)at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java: 89)com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)at com.su 位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)的com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher。)中的n.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)。 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 .sasEventDispatcher.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)com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)at com.sun.javafx.event.EventUtil.fireEvent( EventUtil.java:54)at javafx.event.Event.fireEvent(E vent.java:198)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)在com.sun.javafx.tk.quantum.GlassViewEventHandler的$ javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)com.sun中的$ MouseEventNotification.run(GlassViewEventHandler.java:380) .javafx.tk.quant.GantViewEventHandler $ 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)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)位于com.sun.glass.ui.win.win.WinApplication的com.sun.glass.ui.View.notifyMouse(View.java:937)。 _runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191)at java.lang.Thread.run(Unknown Source)引起:java.lang.reflect。位于sun.reflect.NativeMethodAccessorImpl.invoke(本机方法)的sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)位于java.lang.reflect.Method.invoke(未知来源)的InvocationTargetException )at.reflect.misc.Trampoline.invoke(未知来源)at sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(来自javafx.fxml.FXMLLoader的sun.reflect.misc.MethodUtil.invoke(未知来源)的未知来源。$ MethodHandler.invoke(FXMLLoader.java:1771)... 48更多引起:java.lang.IllegalStateException:位置是没有设置。 at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)at localmart.view.AuthController.Register(AuthController.java:22)... 58更多

Main.java

package localmart.view;
import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {
    private Stage primaryStage;
    private AnchorPane mainLayout;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Local Mart");
        showMainView();

    }

    private void showMainView() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/login.fxml")); // login
                                                                        // form
        try {
            mainLayout = loader.load();
            Scene scene = new Scene(mainLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

controller.java

package localmart.view;

    import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;

public class AuthController {
    @FXML AnchorPane anchorPane;

@FXML
private void RegisterStage() throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("reg.fxml"));
    Pane registerPane = (Pane) fxmlLoader.load();
    try {
        anchorPane.getChildren().clear();
        anchorPane.getChildren().add(registerPane);
    } catch (Exception e) {
        e.printStackTrace();
    }
}   
}

我的问题如何在main.java中加载注册表单和单击注册按钮我需要切换registerform

我将controller,onaction,fx:id设置为login.fxml

你应该有一个AnchorPanelogin.fxml这将是父,将是所有其他元素的容器,我设置anchorPaneid是的Anchorpane (你可以选择你的)。 完成后,您只需要在控制器类中添加这些代码部分

@FXML AnchorPane anchorPane;

@FXML
private void RegisterStage() throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("reg.fxml"));
    Pane registerPane = (Pane) fxmlLoader.load();
    try {
        anchorPane.getChildren().clear();
        anchorPane.getChildren().add(registerPane);
    } catch (Exception e) {
        e.printStackTrace();
    }
}   

用您想要加载的所需fxml替换reg.fxml

暂无
暂无

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

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