【问题标题】:javafx Anonymous Application classjavafx 匿名应用程序类
【发布时间】:2013-05-19 16:29:47
【问题描述】:

我习惯了 Swing 并且正在探索 javafx。在 Swing 中,我将创建一个扩展 Jpanel 的类,然后能够使用该类中创建 JFrame 的几行代码来测试该类。

所以在 javafx 中,我认为我可以扩展 Scene 或 Group,然后能够在 main 中创建一个匿名 Application 类,但失败了:

线程“主”java.lang.RuntimeException 中的异常:错误:类 test.Test 不是 javafx.application.Application 的子类 在 javafx.application.Application.launch(Application.java:211) 在 test.Test.main(Test.java:59)

我不想继承 Application,因为我想为很多场景/组遵循这种模式,并且只能有一个 Application 对象。

当这不起作用时,我想我可以编写一个简单的类来扩展 Application 然后基于提供的参数,使用反射来创建我的场景,但这也不起作用,因为没有默认构造函数场景... Group 有一个默认构造函数,所以也许我需要继承它而不是 Scene?

必须有办法做到这一点……这一直是 java 101 测试和单个类的方法。有没有人这样做过?关于如何完成我在这里尝试做的任何想法或想法?

java版本“1.7.0_21”
Java(TM) SE 运行时环境(内部版本 1.7.0_21-b11)
Java HotSpot(TM) 64 位服务器 VM(内部版本 23.21-b01,混合模式)

这是我的代码:

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 );
   }
}

【问题讨论】:

    标签: javafx anonymous-inner-class


    【解决方案1】:

    请注意,launch 是一个静态方法,因此它不知道您在您创建的匿名应用程序实例上调用它!

    我的最佳想法是让你的代码看起来像这样:

    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();
       }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2015-03-28
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多