简体   繁体   English

JavaFX Canvas和BorderPane

[英]JavaFX Canvas and BorderPane

I'm currently working on a project in JavaFX and I'm doing the GUI with BorderPane. 我目前正在使用JavaFX开发一个项目,并且正在使用BorderPane做GUI。 I have successfully created a menu and accordion, and added them to the positions I wanted (top and right). 我已经成功创建了一个菜单和手风琴,并将它们添加到我想要的位置(顶部和右侧)。

Now I've created a class that extends Canvas and want to add it to the left side, but it doesn't seem to be working. 现在,我创建了一个扩展Canvas的类,并希望将其添加到左侧,但似乎无法正常工作。

What I need is the app to have a menu, extendable options on the right (accordion) and space to draw images on the remaining space (left). 我需要的是该应用程序具有菜单,右侧的可扩展选项(手风琴)和在其余空间上绘制图像的空间(左侧)。

Can anyone shed some light? 谁能阐明一些想法?

Adding some code! 添加一些代码!

public class PainelCanvas extends Canvas implements DesenhoCanvas {

    //ATRIBUTOS
    Canvas canvas;

    //CONSTRUTOR
    public PainelCanvas() {
        canvas = new Canvas(400, 400);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        desenhar(gc);
    }

    @Override
    public void desenhar(GraphicsContext gc) {
        gc.setFill(Color.GREEN);
        gc.fillText("adsads", 20, 30);
    }

}

In my main class, I'm creating an object of PainelCanvas and adding it to my BorderPane. 在我的主类中,我将创建一个PainelCanvas对象,并将其添加到我的BorderPane中。

BorderPane root = new BorderPane();
PainelMenu menu = new PainelMenu();
PainelCanvas canvas = new PainelCanvas();
PainelAccordion painel = new PainelAccordion();

//Definir localização dos vários elementos gráficos
root.setTop(menu);  
root.setLeft(canvas);
root.setRight(painel);

Your canvas has no width or height set, and no content. 您的画布没有设置宽度或高度,也没有内容。

Note that you do 请注意,您确实

public class PainelCanvas extends Canvas ...

and

PainelCanvas canvas = new PainelCanvas();
// ...
root.setLeft(canvas);

So PainelCanvas is a Canvas and is the Canvas you add to your BorderPane . 因此PainelCanvas Canvas并且是您添加到BorderPaneCanvas

Inside the PainelCanvas you create another Canvas : PainelCanvas内,您可以创建另一个Canvas

canvas = new Canvas(400, 400);

and add some content to it: 并添加一些内容:

GraphicsContext gc = canvas.getGraphicsContext2D();
// ...
gc.fillText(...);

But that canvas is never added to the BorderPane . 但是该画布从未添加到BorderPane

If you really want to extend Canvas (not really recommended) you should do 如果您真的想扩展Canvas (不推荐),则应该这样做

public class PainelCanvas extends Canvas implements DesenhoCanvas {
    public PainelCanvas() {
        super(400, 400);
        GraphicsContext gc = this.getGraphicsContext2D();
        desenhar(gc);
    }
    // desenhar(...) method as before
}

But I prefer not to subclass Node classes unless really necessary ("favor aggregation over inheritance"). 但是,除非确实有必要,否则我不希望对Node类进行子类化(“有利于聚合而不是继承”)。

Recommended solution: 推荐的解决方案:

public class PainelCanvas implements DesenhoCanvas {
    private Canvas canvas ;
    public PainelCanvas() {
        canvas = new Canvas(400, 400);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        desenhar(gc);
    }

    // desenhar(...) as before...

    public Node getView() {
        return canvas ;
    }
}

and then: 接着:

root.setLeft(canvas.getView());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM