Saturday, December 10, 2016

Adding a screen

If you read a book about JavaFX, you will find the Scene class. This class is a container type that holds objects displayed inside the Stage. The object of Scene contains another container : Group. The object of Group binds another objects. Those objects are visible in JavaFX Application. Now consider this :

  1. We watch a movie titled The Silence of the Lambs. 
  2. It has an act of conversation between Hannibal Lecter and Clarice Starling. The act is in maximum security jail room. 
  3. This room has a thick bullet proof partition glass, two chairs, a bed several sketch drawings. In this room we have 2 persons : Hannibal Lecter and Clarice Starling.
The movie is analogous to the Stage class. The conversation between Hannibal Lecter and Clarice Starling is analogous to Scene. Finally, things displayed in the scene are analogous to the Group that contains many scene objects int conversation scene.

Now we have an application that display the Hello World Text :


package javafxhelloworld;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;


/**
 *
 * @author Joko Adianto
 */
public class JavaFXHelloWorld extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("Hello World");        
        Group group = new Group();
        group.getChildren().add(label);        
        Scene scene = new Scene(group, 300, 250);        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

The screenshot for this application is depicted below



The container Group holds another objects. In the example above it holds a Label. The object label contains a text value. getChildren().add(l...) is a statement for binding objects to a particular Group.

No comments:

Post a Comment