在两个锚定窗格之间切换

时间:2017-11-01 09:26:59

标签: java javafx scenebuilder

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

我的问题是如何以简单的方式执行此操作?loginForm with signup button

enter image description here

ERRRO IS

  

线程中的异常" JavaFX应用程序线程"   了java.lang.RuntimeException:   java.lang.reflect.InvocationTargetException at   javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1774)at at   javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at   javafx.event.Event.fireEvent(Event.java:198)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)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)     在   com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在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 at   javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at   javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at at   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:380)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:294)     在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)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)     在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)     在java.lang.Thread.run(未知来源)引起:   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.reflect.misc.Trampoline.invoke(未知来源)at   sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.reflect.misc.MethodUtil.invoke(未知来源)at   javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1771)...   48更多引起:java.lang.IllegalStateException:位置不是   组。在javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)at   javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)at 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

1 个答案:

答案 0 :(得分:2)

AnchorPane中的login.fxml将成为父级,并且将成为所有其他元素的容器,我将anchorPane设置为id Anchorpane {1}}(你可以选择你的)。完成后,您只需要在控制器类中添加这些代码部分

@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();
    }
}   

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