[英]How do I make a class in JavaFX 2.0 a usable Node?
I am new to JavaFX so please don't hesitate to explain basic concepts. 我是JavaFX的新手,所以请随时解释基本概念。 I am able to create a simple GUI and I have a
TabPane
with two tabs in my program. 我能够创建一个简单的GUI,并且在程序中有一个带有两个选项卡的
TabPane
。 My question is how do I create a class that can be used as a node? 我的问题是如何创建可用作节点的类?
For example, tab.setContent(stuff)
would add stuff
to a tab
, tab
assuming of course that stuff
is a Node
. 例如,
tab.setContent(stuff)
将增加stuff
到一个tab
, tab
假设当然这stuff
是一个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? 假设我创建了一个名为
Clock
的类,并想将其实例添加到tab
,我该怎么做?
The clock should be a graphical object. 时钟应为图形对象。 I'm new to graphical programming so references to Swing etc. won't be particularly helpful.
我是图形编程的新手,因此对Swing等的引用不会特别有用。
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();
}
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类并向其中添加actionListener来完成。
tab.setContent(new Clock());
Hope this helped. 希望这会有所帮助。
Please add a method in clock, then also make a constructor that calls this method. 请在时钟中添加一个方法,然后制作一个调用此方法的构造函数。 put all
全部放
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);
inside that methode. 在那个方法里面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.