Analysis and use of javaFX2.0 form (Stage)

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

Form function

javaFX provides some control functions of the window through Stage, and provides common window functions such as setting window size, full screen, full screen shortcut key setting, focusing, keeping top, showing, hiding and so on.

The Stage can only be obtained and controlled in the JavaFX Application thread. I checked the source code of javaFX and found that JavaFX has performed thread check on the form control. Only the JavaFX Application thread is allowed to control, and other threads cannot control the form of javaFX. Control and call.

JavaFX thread structure analysis, please refer to the first article: javaFX2.0 thread structure analysis

Form demo code

If you really start to use JavaFX, you will find that JavaFX's form design is very inflexible and has many restrictions. For example, nodes can only be created in the javaFX thread, and other threads are not allowed to operate the form stage. Of course, there are designers’ own considerations. For example, the official documentation notes that this is to ensure the exclusivity of the javafx thread to the form and prevent thread problems like the previous awt. However, after bloggers have used it for a period of time, it is obvious that this solution to solve a problem has more problems, which is obviously not suitable. It not only greatly increases the complexity of other threads for window control, but also brings A more complex code structure is here, which is undoubtedly not worth the loss. I hope that the later versions will be further optimized.

public class StageDemo extends Application {

double minWidth=800,minHeight=600;

static Stage stage;//The form, if you want to operate the api of the form, you must execute it in the javaFX Application thread, and the execution of other threads will throw exceptions.

public static Scene scene;//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.

String title="eguid's form Stage 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, current thread:"+Thread.currentThread().getName());

        Group root=new Group();      

        scene=new Scene(root, 2000,2000,Color.WHITE);//The scene cannot be in
        super.init();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {         Console.log("Open form, current thread:"+Thread.currentThread().getName());         stage=primaryStage;         primaryStage.setScene(scene);         Console. log(scene);         // stage/form         primaryStage.setTitle(title);         // set the maximum and minimum range of the form to prevent the window from being too small and too large.         primaryStage.setMinWidth(minWidth);         primaryStage.setMinHeight(minHeight);








        //Get the maximum resolution of the current screen, a self-implemented tool
        int maxWidth = ScreenUtil.getWidth(), maxHeight = ScreenUtil.getHeight();
        primaryStage.setMaxWidth(maxWidth);//Set the maximum width
        primaryStage.setMaxHeight(maxHeight);/ /Set the maximum height
        Console.log("Maximum resolution:" + maxWidth + "," + maxHeight);
        //Set the exit full screen shortcut key
        primaryStage.setFullScreenExitKeyCombination(new KeyCombination() {             @Override             public boolean match(KeyEvent event) {                 String name=event.getCode().getName();                 Console.log("Key name:"+name);                 return super.match(event);             }         });







        
        primaryStage.setFullScreen(true);//Full screen
        primaryStage.setResizable(true);//Allow free adjustment of the window size
        primaryStage.setFocused(true);//Focus on
        primaryStage.setAlwaysOnTop(true);//Keep the window on top
        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);
    }

}

Guess you like

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