2017-03-01 12 views
0

IntelliJ에서이 짧은 그리기 응용 프로그램을 만들었으며 처음으로 SceneBuilder를 사용해 보았습니다. 장면 빌더에서 만든 "sample.fxml"은 내 Main 클래스에로드되지 않으므로 Canvas 등을 직접 Main 클래스 자체에서 직접 만들었습니다. FXML 로더/파일에 무슨 문제가 있습니까? SceneBuilder를 fxmlloader에 연결


<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.canvas.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.geometry.Insets?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <center> 
     <Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" /> 
    </center> 
</BorderPane> 

별로 문서


package sample; 

import javafx.fxml.FXML; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.paint.Color; 

public class Controller { 
    Canvas canvas = new Canvas(800,500); 


    @FXML 
    public void drawCanvas(){ 

     GraphicsContext gc = canvas.getGraphicsContext2D(); 
     gc.setLineWidth(3); 
     gc.setStroke(Color.BLACK); 
     System.out.println("drawCanvas"); 

     try { 
      canvas.setOnMousePressed(event -> { 
       System.out.println("Mouse click"); 
       gc.beginPath(); 
       gc.lineTo(event.getSceneX(), event.getSceneY()); 
       gc.stroke(); 
      }); 

      canvas.setOnMouseDragged(event -> { 
       System.out.println("Mouse dragged"); 
       gc.lineTo(event.getSceneX(), event.getSceneY()); 
       gc.stroke(); 
      }); 
     }catch (Exception e){ 
      System.out.println(e); 
      System.exit(0); 
     } 

    } 
} 

package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Group; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     try { 
//   FXMLLoader load = FXMLLoader.load(getClass().getResource("sample.fxml")); 
//   load.load(); 
      Group root = new Group(); 
      Controller controller = new Controller(); 
      primaryStage.setTitle("Paint app"); 
      primaryStage.setScene(new Scene(root,800,500)); 
      primaryStage.show(); 
      root.getChildren().add(controller.canvas); 

      /*METODER I PROGRAMMET */ 
      controller.drawCanvas(); 

     }catch (Exception e){ 
      System.out.println(e); 
      System.exit(0); 
     } 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 
는 scenebuilder에 존재합니다. 감사합니다.

+0

Scenebuilder가 작성하는 fxml 파일의 노드. fxml 파일에 노드를 만들 때 컨트롤러의 해당 노드에 액세스하려면 '@FXML NodeType nodeID;'를 사용해야합니다. – Sedrick

+0

Sedricks가 게시 한 것으로 변경하여이 오류가 발생했습니다. "자식 : 자식 노드가 null입니다. parent = Group @ 3d19656b [styleClass = root]" – byblix

답변

0

나는 당신의 fxml과 컨트롤러를 가져 와서 Netbeans에서 만든 프로젝트에 추가했습니다.

This is your problem:

변경 :

Canvas canvas = new Canvas(800,500); 

에 :

@FXML Canvas canvas; 

당신의 컨트롤러에

Sample.java

package sample; 

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

public class Sample extends Application 
{  
    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));//You may need so make the s lowercase. 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

} 

Controller.java

package sample; 

import javafx.fxml.FXML; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.canvas.GraphicsContext; 
import javafx.scene.paint.Color; 

public class Controller { 

    @FXML Canvas canvas; 

    @FXML 
    public void drawCanvas(){ 

     GraphicsContext gc = canvas.getGraphicsContext2D(); 
     gc.setLineWidth(3); 
     gc.setStroke(Color.BLACK); 
     System.out.println("drawCanvas"); 

     try { 
      canvas.setOnMousePressed(event -> { 
       System.out.println("Mouse click"); 
       gc.beginPath(); 
       gc.lineTo(event.getSceneX(), event.getSceneY()); 
       gc.stroke(); 
      }); 

      canvas.setOnMouseDragged(event -> { 
       System.out.println("Mouse dragged"); 
       gc.lineTo(event.getSceneX(), event.getSceneY()); 
       gc.stroke(); 
      }); 
     }catch (Exception e){ 
      System.out.println(e); 
      System.exit(0); 
     } 

    } 
} 

Sample.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.canvas.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.geometry.Insets?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <center> 
     <Canvas id="canvas" fx:id="canvas" height="359.0" onMouseClicked="#drawCanvas" onMouseDragged="#drawCanvas" width="394.0" BorderPane.alignment="CENTER" /> 
    </center> 
</BorderPane> 
+0

: "자식 : 자식 노드가 null입니다. parent = Group @ 3d19656b [styleClass = root]" – byblix

0

정말 이유를 모르겠지만, 나는이에게 몇 번을 변경했는데, 실제로 일을하지 : 감사 @Sedrick 제퍼슨


package sample; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Group; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.canvas.Canvas; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     try { 
      Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 
      primaryStage.setTitle("Paint app"); 
      primaryStage.setScene(new Scene(root)); 
      primaryStage.show(); 

     }catch (Exception e){ 
      System.out.println(e); 
      System.exit(0); 
     } 
    } 


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

내 sample.fxml이 대문자입니다. 그것은 아마 문제 일 것입니다. – Sedrick

+1

어쩌면 내가 자본화했는지 기억하지 못할 수도 있습니다. 그러나 어쨌든 당신을 감사하십시오! – byblix