JavaFX-使用其他类作为节点 [英] JavaFX - Using Other Classes As Nodes

查看:72
本文介绍了JavaFX-使用其他类作为节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,并且对是否有可能在主启动函数中将自己类的对象作为Node添加到场景中感到好奇吗?例如,我想在启动函数中创建一个根节点,然后向其中添加一个Button.除了可以在同一启动函数中创建Button之外,还可以实例化一个Object,该Object在其构造函数中创建自己的按钮,然后将其添加到根Node中?这就是我的意思.我有两个正在使用的课程.主酒吧.这是我的主要课程:

I'm new to JavaFX and am curious about whether or not it is possible to add objects of your own classes as Nodes to a scene in the main start function? For example, I would like to create a root Node in my start function and then add a Button to it. Instead of creating the Button in the same start function, is it possible to instantiate an Object, which creates its own button in its constructor, and then add that to the root Node? Here's what I mean. I have two classes that I'm working with. Main and Bar. Here is my main class:

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    Group root = new Group();

    Bar open = new Bar();

    root.getChildren().add(open);

    Scene scene = new Scene(root, 1024,768, Color.BLACK);
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

}

这是我的律师班:

public class Bar extends BorderPane {

public Bar()
{
    Button btn = new Button("hey"); 
}

}

启动应用程序时,会打开一个1024 X 768黑色框,但上面没有按钮.为什么?当我创建Bar对象(打开)时,其默认构造函数将创建一个Button.然后将该Bar对象作为子对象之一添加到main中我的根对象中.如果我的bar对象现在是根对象的子对象之一,并且该根对象已在场景中使用,那么启动应用程序时,是否应该不显示Bar对象的构造函数中的按钮?

When I launch the application, a 1024 X 768 black box open up but there is no Button on it. Why? When I create my Bar object (open), its default constructor creates a Button. That Bar object is then added as one of the children to my root object in main. If my bar object is now one of the root object's children, and the root object is used in the Scene, shouldn't the button from the Bar object's constructor show up when I launch the app?

在更大范围内,这通常是在Javafx中完成的吗?将所有内容放在一个单独的类中并保持主启动函数尽可能整洁还是可以实例化该函数中的各种对象?非常感谢.

On a larger scale, is this typically done in Javafx? Having everything in a separate class and keeping the main start function as clean as possible Or is it ok to instantiate various objects in that function? Thanks so much.

推荐答案

您必须将按钮作为子项添加到 Bar (实际上是 BorderPane )中:

You must add the button as a child to the Bar (which is actually a BorderPane):

public class Bar extends BorderPane {
    public Bar()
    {
        Button btn = new Button("hey");
        setCenter(btn);
    }
}

通常,在构建较大的UI时有两种主要方法:

Generally there are two main approaches when building up larger UIs:

(1)每个UI-Komponent都是 Node 类型(例如您的情况下的 BorderPane )的子类,并建立构造函数中的用户界面.UI-Komponent是JavaFX场景图的一部分.

(1) Every UI-Komponent is a subclass of a Node type (e.g. BorderPane as in your case) and builds up the user interface in its constructor. The UI-Komponent is part of the JavaFX scene graph.

  • 优点:用法简单-只需实例化您的类并将其作为任何子节点放入场景图即可.
  • 缺点:所有UI都在构造函数中创建.控制器代码是用户界面的一部分.

(2)每个UI-Komponent都由不是 Node 类型的子类的类表示. createUI(Pane parent)这样创建UI并将其放入场景图.必须显式调用此方法.

(2) Every UI-Komponent is represented by a class that is not a subclass of a Node type. Instead, a method like createUI(Pane parent) creates the UI and puts it into the scene graph. This method must be called explicitely.

示例:

public class Bar {
    public void createUI(Pane parent) {
        BorderPane pane = new BorderPane();
        Button btn = new Button("hey");
        pane.setCenter(btn);

        parent.getChildren().add(pane);
    }
}

在主应用程序的 start(Stage)方法中:

In the start(Stage) method of the main application:

Bar bar = new Bar();
bar.createUI(root);

当然,复杂的UI组件可能会在内部使用其他UI组件,并从其自己的 createUI(...)调用其 createUI(...)方法>方法.

Of course, a complex UI-component might internally use other UI-components and call their createUI(...) method from its own createUI(...) method.

  • 优点:UI和控制器分离.用于构建UI的显式方法,因此可以在构建UI之前 实例化并配置该组件.
  • 缺点:使用起来更复杂.

这篇关于JavaFX-使用其他类作为节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆