본문 바로가기

JavaFX/1. Getting Started

Hello World! FX


I modified and summarized javaFX tutorial so I could read understand it easily

I hope this could help you understand JavaFX more easily.

Let's start..



Construct the Application


Creating HelloWorld project in netbeans IDE
  1. From the File menu, choose New Project.

  2. In the JavaFX application category, choose JavaFX Application. Click Next.

  3. Name the project HelloWorld and click Finish.

    NetBeans opens the HelloWorld.java file and populates it with the code for a basic Hello World application, as shown in Example 3-1.

    Example 3-1 Hello World

    package helloworld;
     
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
     
    public class HelloWorld extends Application {
        
        @Override
        public void start(Stage primaryStage) {
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
     
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("Hello World!");
                }
            });
            
            StackPane root = new StackPane();
            root.getChildren().add(btn);
    
     Scene scene = new Scene(root, 300, 250);
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
     public static void main(String[] args) {
            launch(args);
        }
    }
    


Here are the important things to know about the basic structure of a JavaFX application:

  • The main class for a JavaFX application extends the javafx.application.Application class. The start() method is the main entry point for all JavaFX applications.

  • A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. Example 3-1 creates the stage and scene and makes the scene visible in a given pixel size.

  • In JavaFX, the content of the scene is represented as a hierarchical scene graph of nodes. In this example, the root node is a StackPane object, which is a resizable layout node. This means that the root node's size tracks the scene's size and changes when the stage is resized by a user.

  • The root node contains one child node, a button control with text, plus an event handler to print a message when the button is pressed.

  • The main() method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main() method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main() method.


Run the Application

  1. In the Projects window, right-click the HelloWorld project node and choose Run.

  2. Click the Say Hello World button.

  3. Verify that the text ”Hello World!” is printed to the NetBeans output window.
    Figure 3-2 shows the Hello World application, JavaFX style.

Figure 3-2 Hello World, JavaFX style

Description of Figure 3-2 follows
Description of "Figure 3-2 Hello World, JavaFX style"


'JavaFX > 1. Getting Started' 카테고리의 다른 글

7 Animation and Visual Effects in JavaFX  (0) 2016.07.30
4 Creating a Form in JavaFX  (0) 2016.07.30