Getting Started with JavaFX Script

上一篇 / 下一篇  2009-06-01 14:07:35 / 个人分类:java fx


Ready to explore the JavaFX Script. programming language? Great! This lesson describes the software that must be installed on your system before you can begin. It also provides NetBeans IDE and command line instructions for compiling and running your first application.
 
Contents
 
Step 1: Download and install the JDK
Step 2: Choose a Development Environment
Step 3 : Download and Install the JavaFX Compiler and Runtime
 

The JavaFX Script. programming language is based on the Java Platform, and as such, requires JDK 5 or JDK 6 (6 is faster) to be installed on your system. If you have not done so already,download and install JDK 6orJDK 5now, before proceeding with this tutorial.

When it comes to choosing a development environment, you have two broad categories of choices: use an Integrated Development Environment (IDE), or use a plain text editor. This choice is entirely a matter of personal taste, but the following summary might help you to make an informed decision.

Generally speaking:

  • An IDE provides a complete development environment all in one place. You download one piece of software, or perhaps aplug-into that software, which provides everything you need to compile/run/debug your application. IDEs present the most commonly used functions as Graphical User Interface (GUI) elements, and offer many useful features, such as automatic code completion and custom source code views. An IDE also gives you immediate feedback on errors and highlights code so that it is easier to understand. The officially supported IDE for the JavaFX Script. programming language isNetBeans IDE 6.5.1. The NetBeans IDE website provides instructions for downloading, installing, and configuring the IDE.

  • A text editor provides simplicity and familiarity. Experienced programmers often rely on their text editor of choice, preferring to work in that environment whenever possible (some editors, likevi, have a rich set of built-in keystroke commands that some programmers simply cannot live without!) If you already have a preferred editor, rest assured that you can download the sample code for each lesson as.fxsource files for use in your editor of choice.

The JavaFX Script. programming language is acompiledlanguage, which means that any source code you write must first be converted into Java bytecode — the language of the Java Virtual Machine — before it can run on your system. This is true regardless of your development environment (be it command line or IDE). After installing the JDK and choosing a development environment, you will need to download and install the JavaFX Script. compiler and runtime. The easiest way to obtain this software is to download the entireJavaFX SDK, which gives you the NetBeans IDE (optional), compiler, runtime, and a number of other tools.

Another way is to simplydownload the latest compiler binaryfrom the openjfx project website. The compiler itself is written in the Java programming language; installing the precompiled binary therefore becomes a matter of extracting the downloaded file and adding thejavafxcandjavafxtools to your path. The complete set of instructions for this approach can be found on thePlanetJFX Wiki.

Finally — if you want to live on the bleeding edge — you can join theOpenJFX Compiler Project, create your own copy of the compiler workspace, and build everything yourself from the compiler source files. (If you choose this approach, you will also need the 1.7.0 version ofApache Ant, plus a recent copy ofMercurial).

If you have chosen NetBeans IDE 6.5.1 as your development environment, you can use the following instructions to create a project for your first script. a simple calculator.

Step 1: Create a New Project

Launch the NetBeans IDE 6.5.1 and chooseFile | New Project.

When the new project wizard appears, chooseJavaFXas the category and pressNextto continue.

This image has been scaled down to fit your screen. Click to enlarge.
 

Step 2: Choose a Project Name and Location

Next, type "Calculator" as the project name. The NetBeans IDE will provide a default location on your system for this project. You can keep this suggestion, or specify a new one. Make sure that "Empty Project" and "Set as Main Project" are selected, but do not check the "Create Main File" checkbox. Press the "Finish" button when done.

This image has been scaled down to fit your screen. Click to enlarge.
 

Step 3: Add a Source File to the Project

The left side of the IDE contains a file browser window as shown below. You can see that the Calculator project exists, but currently has no source files:

 

To add a source file to the project, chooseFile | New File. SelectJavaFXas the category andEmpty JavaFX Fileas the file type:

This image has been scaled down to fit your screen. Click to enlarge.
 

Next, type "Calculator" as the file name, but leave the package selection empty. You will see a package warning at the bottom of the screen, but ignore this for now; throughout most of this tutorial, we will be placing code into the default package. Press the "Finish" button when done.

This image has been scaled down to fit your screen. Click to enlarge.
 

Step 4: Paste Source Code, Compile, and Run the Application!

The file browser now showsCaclulator.fxas part of the the default package. The source code editor (right hand pane) now contains some default code, which you can safely erase:

This image has been scaled down to fit your screen. Click to enlarge.
 

In its place, paste in the contents ofcalculator.fx. Certain keywords are now highlighted, showing that the editor now recognizes the syntax of the JavaFX Script. programming language:

This image has been scaled down to fit your screen. Click to enlarge.
 

Now look for these buttons along the top of the screen:

This image has been scaled down to fit your screen. Click to enlarge.
 

Press the green button in the center to compile and run the application:

This image has been scaled down to fit your screen. Click to enlarge.
 

You should see output similar to the above. If so, then congratulations, your project works!

If you will be working from the command line only, savecalculator.fxto a directory of your choice. Assuming that the JDK and JavaFX SDK are already installed and in your path, you should be able to compile this program with the following command:

javafxc calculator.fx
 

After compilation, you will find that the corresponding Java bytecode has been generated and placed into a file namedcalculator.class. You might also notice that another file,calculator$Intf.classhas been created. This supporting file is needed to run the application — you can ignore it, but don't delete it.

You can now run the compiled class in the Java Virtual Machine with the following command:

javafx calculator
 

The output is:

100 + 2 = 102
100 - 2 = 98
100 * 2 = 200
100 / 2 = 50
 

This application may be small, but it introduces you to some important programming language constructs (discussed in the next lesson). Learning these constructs is your first step towards mastering the JavaFX Script. programming language.

(If you want to learn more, please pay attention tohttp://java.sun.com/javafx/1/tutorials/core/


TAG:

 

评分:0

我来说两句

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 58075
  • 日志数: 89
  • 建立时间: 2008-12-13
  • 更新时间: 2010-01-13

RSS订阅

Open Toolbar