如何使JavaFX 2.0中的类成为可用的节点? [英] How do I make a class in JavaFX 2.0 a usable Node?

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

问题描述

我是JavaFX的新手,所以请不要犹豫,解释基本概念。我能够创建一个简单的GUI,我有一个 TabPane ,在我的程序中有两个选项卡。我的问题是如何创建一个可以用作节点的类?

I am new to JavaFX so please don't hesitate to explain basic concepts. I am able to create a simple GUI and I have a TabPane with two tabs in my program. My question is how do I create a class that can be used as a node?

例如, tab.setContent(stuff) stuff 添加到标签标签当然假设 stuff 是一个节点。所以假设我创建了一个名为 Clock 的类,并希望将它的实例添加到选项卡,我该怎么办这样的事情?

For example, tab.setContent(stuff) would add stuff to a tab, tab assuming of course that stuff is a Node. So let's say I create a class called Clock and want to add an instance of it to tab, how do I do such a thing?

时钟应该是一个图形对象。我是图形编程的新手,所以对Swing等的引用不会特别有用。

The clock should be a graphical object. I'm new to graphical programming so references to Swing etc. won't be particularly helpful.

推荐答案

在你的情况下,创建一个时钟类,并用你希望它包含的布局扩展它。例如:

In your case, create a class "Clock" and extend it with a layout you wish it to contain. For example:

public class Clock extends BorderPane{}

然后,您可以使用构造函数为其设置属性或其他节点。

Then, you can set properties or other Nodes to it by using a constructor.

public class Clock extends BorderPane{
     TextArea ta = new TextArea("This is TOP");
     this.setTop(ta);
     Button b1 = new Button("This is button 1");
     Button b2 = new Button("This is button 2");
     HBox box = new HBox();
     box.getChildren().addAll(b1,b2);
     this.setCenter(box);
}

然后,您可以从主程序中调用它:

Then, you would call it from your main program as such:

@Override
public void start(Stage primaryStage){
     primaryStage.setScene(new Scene(new Clock()));
     primaryStage.show();
}

在您的情况下,您可以在按下选项卡时设置场景。这可以使用Tab类并向其添加actionListener来完成。

In your case, you would set the Scene when the tab is pressed. That can be done using the Tab class and adding an actionListener to it.

tab.setContent(new Clock());

希望这会有所帮助。

这篇关于如何使JavaFX 2.0中的类成为可用的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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