【发布时间】:2015-07-04 00:21:33
【问题描述】:
我想在场景构建器中使用自定义组件。
我想将画布嵌入到自定义组件中。所以我尝试改变属性的画布。
这样的画布代码:
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>
请给我建议和提示。
【问题讨论】:
-
我没有尝试将您的代码导入 SceneBuilder,但有一个 quick guide on how to import components into SceneBuilder(以及如何解决导入过程),也许这可能会对您或希望回答这个问题的人有所帮助。
标签: javafx custom-component scenebuilder