2015-04-25 54 views
3

我想在場景構建器中使用自定義組件。
我想嵌入一個畫布到自定義組件。所以我嘗試改變屬性的畫布。
帆布這樣的代碼:javafx場景構建器自定義組件

package test; 

    import javafx.scene.canvas.Canvas; 
    import javafx.scene.canvas.GraphicsContext; 

    public class DrawCanvas extends Canvas{ 
     public DrawCanvas() { 
      draw(); 
     } 

     private void draw() { 
      // TODO Auto-generated method stub 
      double width = getWidth(); 
      double height = getHeight(); 
      GraphicsContext gc = getGraphicsContext2D(); 
      gc.strokeLine(0,0,50,50); 
     } 
    } 


自定義組件代碼:

package test; 

    import java.io.IOException; 
    import javafx.fxml.FXMLLoader; 
    import javafx.scene.layout.BorderPane; 

    public class Test extends BorderPane{ 

     public Test() { 
      super(); 
      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Test.fxml")); 
      fxmlLoader.setRoot(this); 
      fxmlLoader.setController(this); 
      try { 
       fxmlLoader.load(); 
      } catch (IOException exception) { 
       throw new RuntimeException(exception); 
      } 
     } 
    } 


FXML文件:

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

    <?import javafx.scene.layout.BorderPane?> 
    <?import javafx.scene.*?> 
    <?import javafx.scene.control.*?> 
    <?import javafx.scene.canvas.Canvas?> 


    <fx:root xmlns:fx="http://javafx.com/fxml" type="javafx.scene.layout.BorderPane"> 
     <center> 
     </center> 
    </fx:root> 


我這樣嘗試過,但它失敗。

<?import javafx.scene.layout.BorderPane?> 
    <?import javafx.scene.*?> 
    <?import javafx.scene.control.*?> 
    <?import javafx.scene.canvas.Canvas?> 
    <?import org.korecky.myjavafx.fxml10.DrawCanvas?> 
    <fx:root xmlns:fx="http://javafx.com/fxml" type="javafx.scene.layout.BorderPane"> 
     <center> 
      <DrawCanvas ></DrawCanvas> 
     </center> 
    </fx:root> 

請給我建議和提示。

+0

我還沒有試過導入你的代碼SceneBuilder ,但有一個[如何導入組件到SceneBuilder]的快速指南(http://stackoverflow.com/questions/29444698/how-to-create-an-fxml-file-for-an-already-created-new -component式-java-add-add)(以及如何解決導入過程),也許這可能會幫助您或希望回答此問題的人。 – jewelsea

回答

4

你的方法正在爲我工​​作,但你需要創建一個有效的畫布,提供一些尺寸,否則那些將是0x0。例如:

private void draw() { 
    setWidth(50); 
    setHeight(50); 
    GraphicsContext gc = getGraphicsContext2D(); 
    gc.strokeLine(0,0,50,50); 
} 

現在你可以導入你的DrawCanvas組件到SceneBuilder,如@jewelsea建議,你就可以把它拖到場景:

canvas

你可以添加一些屬性的類,如canvasWidthcanvasHeight

public class DrawCanvas extends Canvas { 

    private final GraphicsContext gc; 

    public DrawCanvas() { 
     gc = getGraphicsContext2D(); 
     draw(); 
    } 

    private void draw() { 
     setWidth(canvasWidth.get()); 
     setHeight(canvasHeight.get()); 
     gc.clearRect(0,0,canvasWidth.get(),canvasHeight.get()); 
     gc.strokeLine(0,0,canvasWidth.get(),canvasHeight.get()); 
    } 

    private final DoubleProperty canvasWidth = new SimpleDoubleProperty(50){ 
     @Override 
     protected void invalidated() { 
      draw(); 
     } 
    }; 

    public double getCanvasWidth() { 
     return canvasWidth.get(); 
    } 

    public void setCanvasWidth(double value) { 
     canvasWidth.set(value); 
    } 

    public DoubleProperty canvasWidthProperty() { 
     return canvasWidth; 
    } 
    private final DoubleProperty canvasHeight = new SimpleDoubleProperty(50){ 
     @Override 
     protected void invalidated() { 
      draw(); 
     } 
    }; 

    public double getCanvasHeight() { 
     return canvasHeight.get(); 
    } 

    public void setCanvasHeight(double value) { 
     canvasHeight.set(value); 
    } 

    public DoubleProperty canvasHeightProperty() { 
     return canvasHeight; 
    } 

} 

這將允許您設置它們Inspector面板上:

enter image description here

或在您的FXML文件:

<fx:root type="javafx.scene.layout.BorderPane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"> 
    <center> 
     <DrawCanvas canvasWidth="150" canvasHeight="250" /> 
    </center> 
</fx:root> 
相關問題