javafx匿名应用程序类 [英] javafx Anonymous Application class
问题描述
我习惯了Swing,正在探索javafx。在swing中,我创建了一个扩展Jpanel的类,然后能够在该类中创建一个JFrame的几行代码来测试该类。
I'm used to Swing and am exploring javafx. In swing I'd create a class that extends Jpanel and then be able to test that class with a couple of lines of code in that class that created a JFrame.
所以在javafx中,我想我可以扩展Scene或Group,然后能够在main中创建一个匿名Application类,但是失败了:
So in javafx I thought I could just extend Scene or Group, and then be able to create an anonymous Application class in main but that fails with:
线程main中的异常java.lang.RuntimeException:错误:类test.Test不是javafx.application.Application的子类
at javafx.application.Application.launch(Application.java:211)
at test.Test。 main(Test.java:59)
Exception in thread "main" java.lang.RuntimeException: Error: class test.Test is not a subclass of javafx.application.Application at javafx.application.Application.launch(Application.java:211) at test.Test.main(Test.java:59)
我不想继承应用程序,因为我想对很多场景/群组遵循这种模式而且只能有一个应用程序对象。
I don't want to subclass Application as I want to follow this pattern for lots of Scenes/Groups and there can only be one Application object.
当这不起作用时,我想我可以编写一个扩展Application的简单类,然后根据提供的args,使用反射来创建我的场景但这不起作用,因为没有默认的构造函数一个场景...组有一个默认的构造函数,所以也许我需要子类而不是场景?
When that didn't work, I thought I could write a simple class that extends Application and then based on the args provided, use reflection to create my Scene but that doesn't work either since there is no default constructor for a scene... Group has a default constuctor, so maybe I need to subclass that instead of Scene?
必须有办法做到这一点......这有一直是java 101测试和个人课程的方式。有没有人这样做过?关于如何完成我在这里尝试做的任何想法或想法?
There must be a way to do this... this has always been a java 101 way to test and individual class. Has anyone ever done this? Any thoughts or ideas on how to accomplish what I'm trying to do here?
java版本1.7.0_21
Java(TM) SE运行时环境(版本1.7.0_21-b11)
Java HotSpot(TM)64位服务器VM(版本23.21-b01,混合模式)
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
这是我的代码:
package test;
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.input.*;
import javafx.scene.effect.*;
public class Test extends javafx.scene.Scene
{
public Test( javafx.scene.Group group, int width, int height )
{
super( group, width, height );
GridPane grid = new GridPane();
grid.setVgap( 4 );
grid.setHgap( 10 );
grid.setPadding( new Insets( 5, 5, 5, 5 ) );
final Button button = new Button ("Ok");
final Label notification = new Label ();
final TextField subject = new TextField("");
final TextArea text = new TextArea ("");
final ComboBox priorityComboBox = new ComboBox();
priorityComboBox.getItems().addAll( "Highest", "High", "Normal", "Low", "Lowest" );
priorityComboBox.setValue("Normal");
grid.add(new Label("Priority: "), 0, 0);
grid.add(priorityComboBox, 1, 0);
grid.add(new Label("Subject: "), 0, 1);
grid.add(subject, 1, 1, 3, 1);
grid.add(text, 0, 2, 4, 1);
grid.add(button, 0, 3);
group.getChildren().add( grid );
}
public static void main(String [] args)
{
Application app = new Application()
{
public void start(Stage stage)
{
stage.setTitle( "Test" );
Scene scene = new Test( new Group(), 450, 250);
stage.setScene( scene );
stage.show();
}
};
app.launch( args );
}
}
推荐答案
请请注意,启动是一种静态方法,因此它不知道您是否在您创建的匿名应用程序实例上调用它!
Please note that launch is a static method so it does not know you are calling it on the Anonymous Application-Instance you created!
我最好的想法是让你的代码看起来像这样:
The best idea i have is that you make you code look like this:
public static void main(String [] args)
{
Application.launch( MyApp.class, args );
}
public static class MyApp extends Application {
public void start(Stage stage)
{
stage.setTitle( "Test" );
Scene scene = new Test( new Group(), 450, 250);
stage.setScene( scene );
stage.show();
}
}
这篇关于javafx匿名应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!