javafx Anonymous Application class

帅比萌擦擦* 提交于 2019-12-19 11:27:07

问题


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.

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:

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.

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?

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

Here is my code:

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

回答1:


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


来源:https://stackoverflow.com/questions/16636883/javafx-anonymous-application-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!