2017-03-01 9 views
0

私はこの短い描画アプリケーションをIntelliJで作ったので、初めてSceneBuilderを試しました。シーンビルダーで作成した "sample.fxml"はメインクラスに読み込まれないので、メインクラス自体でCanvasなどを直接作成しました。 FXMLローダー/ファイルで何が間違っていますか?SceneBuilderをfxmlloaderに接続

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

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

    } 
} 

<?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> 

あまりドキュメントは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