>

저는 Java FX 프로젝트를 진행하고 있습니다 두 개의 FXML 파일을 만들었고 Border Pane이라는 주요 고통에 다른 창을로드하고 싶습니다. 테두리 창 중앙 영역에 다른 창을로드하고 싶습니다!

다음은 Java 파일 인 프로젝트의 시작점입니다.

package javafxapplication8;
    import java.io.IOException;
    import javafx.application.Application;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    /**
     *
     * @author Anu
     */
    public class JavaFXApplication8 extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        }
        @FXML
        void handleButtonAction(MouseEvent event) throws IOException {
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }
    }

그런 다음 내 경계 창을 표시하는 기본 FXML 파일이 표시됩니다

<?xml version="1.0" encoding="UTF-8"?>
    <?import com.jfoenix.controls.JFXButton?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.layout.BorderPane?>
    <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" 
                fx:controller="javafxapplication8.FXMLDocumentController">
       <children>
          <BorderPane fx:id="rootStage" layoutX="14.0" layoutY="-14.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
             <left>
                <JFXButton buttonType="RAISED" 
                           onAction="#handleButtonAction"
                            text="Load Pane" BorderPane.alignment="CENTER" />
             </left>
          </BorderPane>
       </children>
    </AnchorPane>

특정 창을 해당 경계 창 중앙 영역에 표시하도록 단추를 설정했습니다.

<left>
   <JFXButton buttonType="RAISED" 
    onAction="#handleButtonAction"
    text="Load Pane" BorderPane.alignment="CENTER" />
</left>

이것은 Border Pane에 포함 된 클래스의 내 컨트롤러 클래스입니다

package javafxapplication8;
    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.control.Label;
    import javafx.scene.layout.BorderPane;
    /**
     *
     * @author Anu
     */
    public class FXMLDocumentController implements Initializable {
        @FXML
        private BorderPane rootStage;
        @FXML
        private void handleButtonAction(ActionEvent event) throws IOException {
            System.out.println("Button Click");
            FXMLLoader loader = new FXMLLoader();  
            loader.setLocation(javafx.stage.Stage.class.getResource("FXML.fxml"));  
            BorderPane newScene = loader.load();  
            rootStage.setCenter(newScene);  
        }
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
            System.out.println("FXMLDocumentController");
        }    
    }

이것은 Border Pane 중앙 영역에 표시하려는 FXML 파일입니다.

       <?xml version="1.0" encoding="UTF-8"?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.text.Font?>
    <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="javafxapplication8.FXMLController">
       <children>
          <Label layoutX="252.0" layoutY="179.0" text="Label">
             <font>
                <Font size="41.0" />
             </font>
          </Label>
       </children>
    </AnchorPane>

그러나 버튼을 클릭하면 많은 오류가 발생합니다!

Executing C:\Users\Anu\Documents\NetBeansProjects\JavaFXApplication8\dist\run922593932\JavaFXApplication8.jar using platform C:\Program Files\Java\jdk1.8.0_172\jre/bin/java
    FXMLDocumentController
    Button Click
    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.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.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.Node.fireEvent(Node.java:8411)
        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)
        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.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:394)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
        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$147(WinApplication.java:177)
        at java.lang.Thread.run(Thread.java:748)
    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:498)
        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:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        ... 48 more
    Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
        at javafxapplication8.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:33)
        ... 58 more

내 프로젝트 구조 :

내가 놓친 곳을 보여줄 수있는 사람이 있습니까? 감사합니다.


  • 답변 # 1

    2 가지 이유가 있습니다

    javafx.stage.Stage.class.getResource("FXML.fxml")
    
    

    와이즈 비즈를 반환  이것이 예외의 원인입니다 :

    null 로 시작하지 않는 자원  클래스와 관련하여 해결됩니다. 즉,이 경우 java가 찾고있는 자원은 / 입니다.  자원의 실제 위치 대신

    /javafx/stage/FXML.fxml  어떤 이유로 항아리에있는 리소스를 확인할 수없는 다른 클래스 로더를 사용하는 것 같습니다. 항아리에 액세스 할 수있는 클래스 로더가로드 한 다른 클래스를 사용해야합니다 (예 : Stage 사용

    FXMLDocumentController.class
    
    
    <시간>

    참고 :새로운 loader.setLocation(FXMLDocumentController.class.getResource("/javafxapplication8/FXML.fxml")); 를 얻으려면청소 및 빌드를 사용해야 할 수도 있습니다.  항아리에 보관하십시오.

    .class

관련 자료

  • 이전 Datalog 입력 형식을 사용하여 Z3의 BitVec에서 1을 센다
  • 다음 java - 이진 문자열을 정수로 변환하는 동안 NumberFormatException