从javafx中的控制器调用其他类方法

时间:2013-06-22 15:37:07

标签: java controller javafx fxml

我目前正在JavaFx中执行一个应用程序,并且我从Controller类调用主类方法。我检查了Oracle提供的示例示例,并且在该示例中它正常工作。 这是我的代码: 主要课程:

public class Boiler_New extends Application {

private Stage stage;
@Override
public void start(Stage primarystage) throws Exception {
    stage = primarystage;
    stage.setTitle("Login");
    call1("Login.fxml");
    stage.show();


}
public void call1(String fxml) throws IOException{
    FXMLLoader loader = new FXMLLoader();
    InputStream in = Boiler_New.class.getResourceAsStream(fxml);
    loader.setBuilderFactory(new JavaFXBuilderFactory());
    loader.setLocation(Boiler_New.class.getResource(fxml));
    AnchorPane root = (AnchorPane)loader.load(in);
     Scene scene = new Scene(root);
     stage.setScene(scene);

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

控制器:

public class LoginController extends AnchorPane implements Initializable {

private Boiler_New application ;
@FXML
private Label label;

@FXML
private AnchorPane login_panel;

@FXML
private Button login_button;

@FXML
private ImageView close_button;


public void setApp(Boiler_New application){
    this.application = application;
}

@FXML
public void login_act(ActionEvent event) throws Exception {

    application.call1("content.fxml");

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO

}    
}

跟踪:

Executing boiler_new.Boiler_New from C:\Users\BlackWorld\Documents\NetBeansProjects   \Boiler_New\dist\run1175007848\Boiler_New.jar using platform C:\Program Files\Java\jdk1.7.0_25/bin/java
***java.lang.RuntimeException: java.lang.reflect.InvocationTargetException***
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at  com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6863)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
at com.sun.glass.ui.View.notifyMouse(View.java:922)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:724)
***Caused by: java.lang.reflect.InvocationTargetException***
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1435)
... 44 more
***Caused by: java.lang.NullPointerException***
at boiler_new.LoginController.login_act(LoginController.java:50)
... 54 more
Exception in thread "JavaFX Application Thread" Deleting directory C:\Users\BlackWorld\Documents\NetBeansProjects\Boiler_New\dist\run1175007848

请帮帮我。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,要解决它,请执行以下操作:

  1. 删除“call1(”Login.fxml“);”来自start()方法。
  2. 从Boiler_new中删除public void call1(String fxml)。
  3. 在LoginController中添加一个新函数,如下所示:

    private void openNewWindow(Event event)
    {
    
     Parent root;
     try {                    
        URL url = getClass().getResource("Content.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(url);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        root = (Parent)fxmlLoader.load(url.openStream());            
        Stage stage = new Stage();
        stage.setScene(new Scene(root, 1000, 800));
        stage.show();
        ((Node)(event.getSource())).getScene().getWindow().hide();
    
      } catch (IOException e) {
          e.printStackTrace();
      }
    }
    
  4. 调用此函数而不是application.call1(“content.fxml”);

    @FXML
     public void login_act(ActionEvent event) throws Exception {
    
         openNewWindow(event);
     }
    

    我确信它运作正常。