Analysis and use of javaFX2.0 scene (Scene), realization of multi-scene switching

Preface

JavaFX is a powerful graphics and multimedia processing toolkit collection, which allows developers to design, create, test, debug and deploy rich client programs, and is as cross-platform as Java.

Composition structure

Node structure

When it comes to Scene, you have to talk about the node structure of javaFX. JavaFX designs all controls as Node.

Form (Stage)------Settings------>One Scene (Scene)-----Add------>Multiple nodes (Node, can be UI components, pictures, Audio and video, web browser, etc.)
 

Simple scene function realization

public class SceneDemo extends Application {

String title="eguid simple scene Scene function demonstration, please indicate the source and author name for reprinting, blog. eguid. cc original technical article";

    @Override
    public void init() throws Exception {         Console.log("Some initialization before the form is opened, the current thread:"+Thread.currentThread().getName());         super.init();     }


    @Override
    public void start(Stage primaryStage) throws Exception {         Console.log("Open form, current thread:"+Thread.currentThread().getName());         primaryStage.setTitle(title); // set title

        Group root=new Group();

        //Scene, the scene must be created after the form Stage is initialized. It can only be created in the init() method of the javaFX launcher thread and the javaFX Application thread. Other threads will throw exceptions.

        Scene scene=new Scene(root, 2000,2000,Color.WHITE);//The scene cannot be created outside the javaFX thread

        primaryStage.setScene(scene);//Set the scene
        primaryStage.show();//Display form
    }

    @Override
    public void stop() throws Exception {         Console.log("Close the form, current thread:"+Thread.currentThread().getName());         super.stop();     }


    public static void main(String[] args) {
        Console.log("启动程序");
        Application.launch(args);
    }

}

 

Multi-scene switching

Although only one scene can be set in the stage (Stage), we can switch between multiple scenes by setting the scene. For example, let's take a simple chat software as an example. We need two scenarios to simulate the approximate look:

  • Login scene (used to enter the user name and password to log in)
  • Main scene (the main operation scene that the user enters after logging in, viewing the friend list, etc.)

The window just opened shows the "login scene". After a fierce operation, click the login button to enter the "main scene". How to realize this scene switching function.

Note: Although we can use multiple forms to achieve similar scene switching functions, the resource consumption of creating and destroying forms is obviously much larger than that of creating scenes. The first article has analyzed the JavaFX Forms contain three threads (launcher thread, JavaFX rendering thread and JavaFX Application thread), and our multi-scene switching is actually sharing a form, which consumes less resources.

 

Multi-scene switching function realization

public class SceneChangeSample extends Application {     String title="eguid simple scene Scene function demonstration, please indicate the source and author's name for reprint, blog. eguid. cc original technical article";

    @Override
    public void init() throws Exception {         Console.log("Some initializations before the form is opened, current thread:" + Thread.currentThread().getName());         super.init();     }


    @Override
    public void start(Stage primaryStage) throws Exception {         Console.log("Open form, current thread:" + Thread.currentThread().getName());         primaryStage.setTitle(title); // set title

        Group root = new Group();

        // Scenes. Scenes must be created after the form Stage is initialized. They can only be created in the init() method of the javaFX launcher thread and the javaFX
        // Application thread. Other threads will throw exceptions.

        Scene scene = new Scene(root, 2000, 2000, Color.WHITE);// The scene cannot be created outside the javaFX thread

        // Create the second scene
        Group second = new Group();
        Scene secondScene = new Scene(second, 800, 600, Color.BLACK);

        EventHandler<Event> eventHandler = new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                EventType<? extends Event> type = event.getEventType();
                String name = type.getName();
                Console.log("事件名称:" + name);

// KeyEvent.KEY_PRESSED
                switch (name) {                 case "MOUSE_CLICKED":                     // The animation effect is triggered after the key is pressed, and the application working thread is not affected at all after playback, indicating that it is completely feasible.                     // The logic calculation can be processed in the event                     Console.log(" Mouse click");                     break;                 case "KEY_PRESSED":                     // Any keyboard button triggers the scene switch and switches to the second scene                     primaryStage.setScene(secondScene);                     break;                 }                 event.consume();             }         };         scene.addEventHandler( Event.ANY, eventHandler);// Binding event













        primaryStage.setScene(scene);// Set the first scene as the display scene
        primaryStage.show();// Display the form
    }

    @Override
    public void stop() throws Exception {         Console.log("Close the form, current thread:" + Thread.currentThread().getName());         super.stop();     }


    public static void main(String[] args) {
        Console.log("启动程序");
        Application.launch(args);
    }

}

 

 

 

Guess you like

Origin blog.csdn.net/eguid/article/details/94326636