Movatterモバイル変換


[0]ホーム

URL:


JavaFX Documentation Home >Getting Started with JavaFX

Release: JavaFX 2.2.40

Last Updated: September 2013

Download as PDF

[+] Show/Hide Table of Contents

About This Tutorial

1 Hello World, JavaFX Style

2 Creating a Form in JavaFX

3 Fancy Forms with JavaFX CSS

4 Using FXML to Create a User Interface

5 Animation and Visual Effects in JavaFX

6 Deploying Your First JavaFX Application

Application Files

View Source Code

Download Source Code

Profiles

Gail Chappell

Technical Writer, Oracle

The picture of the authorGail is part of the JavaFX documentation team and enjoys working on cutting-edge, innovative documentation.

Nancy Hildebrandt

Technical Writer, Oracle

The picture of the authorNancy is a technical writer in the JavaFX group. She has a background in content management systems, enterprise server-client software, and XML. She lives on 480 acres in the middle of nowhere with horses, a donkey, dogs, cats, and chickens, and stays connected by satellite.

We Welcome Your Comments

Send usfeedback about this document.

If you have questions about JavaFX, please go to theforum.

Getting Started with JavaFX

Previous
Next

1 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 7.3. Before you begin, ensure that the version of NetBeans IDE that you are using supports JavaFX 2. See theSystem Requirements for details.

Construct the Application

  1. From theFile menu, chooseNew Project.

  2. In theJavaFX application category, chooseJavaFX Application. ClickNext.

  3. Name the projectHelloWorld and clickFinish.

    NetBeans opens theHelloWorld.java file and populates it with the code for a basic Hello World application, as shown inExample 1-1.

    Example 1-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 {    public static void main(String[] args) {        launch(args);    }        @Override    public void start(Stage primaryStage) {        primaryStage.setTitle("Hello World!");        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);        primaryStage.setScene(new Scene(root, 300, 250));        primaryStage.show();    }}

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

  • The main class for a JavaFX application extends thejavafx.application.Application class. Thestart() 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 JavaFXStage class is the top-level JavaFX container. The JavaFXScene class is the container for all content.Example 1-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 aStackPane 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.

  • Themain() 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 themain() 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 themain() method.

Figure 1-1 shows the scene graph for the Hello World application. For more information on scene graphs seeWorking with the JavaFX Scene Graph.

Figure 1-1 Hello World Scene Graph

Description of Figure 1-1 follows
Description of "Figure 1-1 Hello World Scene Graph"

Run the Application

  1. In the Projects window, right-click theHelloWorld project node and chooseRun.

  2. Click the Say Hello World button.

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

Figure 1-2 Hello World, JavaFX style

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

Where to Go Next

This concludes the basic Hello World tutorial, but continue reading for more lessons on developing JavaFX applications:

Previous
Next

Copyright © 2008, 2013, Oracle and/or its affiliates. All rights reserved.
Legal Notices
    Accessibility Statement

[8]ページ先頭

©2009-2025 Movatter.jp