Hello World, JavaFX Style

Hello World, JavaFX Style

 

The best way to teach you what it is like to create and build a JavaFX application is with a ”Hello World” application. An added benefit of this tutorial is that it enables you to test that your JavaFX technology is properly installed.

The tool used in this tutorial is NetBeans IDE 8.0 Before you begin, ensure that the version of NetBeans IDE that you are using supports JavaFX 8.

Construct the Application

  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.


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 from All in one!");
            }
        });
        
        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);
    }
}

 

No comments:

Post a Comment