Thursday, May 30, 2013

JavaFX Hello World

This is my first JavaFX application. It is kinda similar to the first Hello World application in the tutorials but I made a few modifications (not bigger modifications but a considerable ones).
Hope you guys enjoy coding in fx.

SOURCE CODE (HELLO WORLD)
package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class Main extends Application{
    public static void main(String[] args)
    {
        System.out.println("Launching Hello World Application");
        launch(args);
    }
    @Override
    public void start(final Stage primaryStage) {
        System.out.println("Preparing the stage");
        primaryStage.setTitle("Hello World");
        System.out.println("Title set to Hello World");
        System.out.println("Creating a new button and setting button text to Say 'Hello World'");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        System.out.println("Creating event handler for Hello World Button");
        btn.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World");
            }

        });
        System.out.println("Creating another Button to exit");
        Button btn_exit = new Button();
        btn_exit.setText("Exit");
        System.out.println("Creating event handler for exit button");
        btn_exit.setOnAction(new EventHandler() {
            @Override
            public void handle(ActionEvent actionEvent) {
                System.out.println("Closing the applicaion");
                primaryStage.close();
            }
        });

        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25,25,25,25));
        Scene scene = new Scene(grid,300,275);

        grid.add(btn,0,1);
        grid.add(btn_exit,1,1);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

OUTPUT SNAPSHOTS:



Keep visiting for more posts.

No comments:

Post a Comment