Java FX Script - Một cái nhìn tổng quan
EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER JavaFX™ Script: Dynamic Java™ Scripting for Rich Internet/Client-Side Applications Inside Java™ Beginning J2EE™ 1.4 Pro J2EE™ 1.4 Beginning Java™ EE ™ Author of JavaFX Script Apress’s firstPress series is your source for understanding cutting-edge technology Short, highly focused, and written by experts, Apress’s firstPress books save you time and effort They contain the information you could get based on intensive research yourself or if you were to attend a conference every other week—if only you had the time They cover the concepts and techniques that will keep you ahead of the technology curve Apress’s firstPress books are real books, in your choice of electronic or print-on-demand format, with no rough edges even when the technology itself is still rough You can’t afford to be without them Available as a PDF Electronic Book or Print On Demand Dear Reader, JavaFX Script was revealed at JavaOne 2007 as the tool that will enable developers to create rich content using the JavaFX family of products from Sun Microsystems I wrote JavaFX™ Script: Dynamic Java™ Scripting for Rich Internet/Client-Side Applications because I am excited about the power and simplicity of using JavaFX Script to create rich internet applications that will run on many different platforms, including personal computers and mobile phones I began developing Java applications in 1995 and have witnessed the capabilities and complexities of Java increase over time I recognized the potential in JavaFX Script straight away when I saw how it can give content designers and application developers a simple, but powerful, language that has all the capability of Java behind it For example, in JavaFX Script a simple declarative expression can define a user interface complete with platform-independent layout managers Behind-the-scenes, Java facilities such as layout managers and Swing components are automatically employed to implement the desired user interface My tutorial-style book will have you developing applications with JavaFX Script as quickly as possible I lead you step-by-step through JavaFX Script concepts, with each concept building on your knowledge of the previous one Exercises and sample solutions test your understanding throughout, help you learn even faster and, combined with the logical style of the tutorial, also provide a valuable course textbook or training companion Complex information on JavaFX Script constructs, such as operators, are presented in tabular format which makes the book an easy-to-reference guide that you will turn to again and again JavaFX Script is very new, and still changing As it does, this book will be updated to reflect these changes It is my hope that you’ll share my excitement about JavaFX Script and the potential it offers, and that you’ll enjoy working through the book to reach it JavaFX Script ™ Dynamic Java Scripting for Rich Internet/Client-Side Applications ™ CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER Introduction to JavaFX Quick Start Creating User Interfaces in JavaFX 29 Creating JavaFX Classes and Objects 81 More UI Fun Including 2D Drawing 147 216 Regards, pages James L (Jim) Weaver, SCJD October, 2007 Weaver Related Titles SOURCE CODE ONLINE www.apress.com User level: Beginner–Intermediate James L Weaver this print for content only—size & color not accurate spine = 0.456" 216 page count About firstPress Apress's firstPress series is your source for understanding cutting-edge technology Short, highly focused, and written by experts, Apress's firstPress books save you time and effort They contain the information you could get based on intensive research yourself or if you were to attend a conference every other week––if only you had the time They cover the concepts and techniques that will keep you ahead of the technology curve Apress's firstPress books are real books, in your choice of electronic or print-on-demand format, with no rough edges even when the technology itself is still rough You can't afford to be without them JavaFX™ Script: Dynamic Java™ Scripting for Rich Internet/Client-Side Applications Dear Reader, JavaFX Script was revealed at JavaOne 2007 as the tool that will enable developers to create rich content using the JavaFX family of products from Sun Microsystems I wrote JavaFX™ Script: Dynamic Java™ Scripting for Rich Internet/Client-Side Applications because I am excited about the power and simplicity of using JavaFX Script to create rich internet applications that will run on many different platforms, including personal computers and mobile phones I began developing Java applications in 1995 and have witnessed the capabilities and complexities of Java increase over time I recognized the potential in JavaFX Script straight away when I saw how it can give content designers and application developers a simple, but powerful, language that has all the capability of Java behind it For example, in JavaFX Script a simple declarative expression can define a user interface complete with platform-independent layout managers Behind-the-scenes, Java facilities such as layout managers and Swing components are automatically employed to implement the desired user interface My tutorial-style book will have you developing applications with JavaFX Script as quickly as possible I lead you step-by-step through JavaFX Script concepts, with each concept building on your knowledge of the previous one Exercises and sample solutions test your understanding throughout, help you learn even faster and, combined with the logical style of the tutorial, also provide a valuable course textbook or training companion Complex information on JavaFX Script constructs, such as operators, are presented in tabular format which makes the book an easy-to-reference guide that you will turn to again and again JavaFX Script is very new, and still changing As it does, this book will be updated to reflect these changes It is my hope that you’ll share my excitement about JavaFX Script and the potential it offers, and that you’ll enjoy working through the book to reach it Regards, James L (Jim) Weaver, SCJD October, 2007 JavaFX Script ™ Dynamic Java™ Scripting for Rich Internet/Client-Side Applications JAMES L WEAVER JavaFX™ Script: Dynamic Java™ Scripting for Rich Internet/Client-Side Applications Copyright © 2007 by James L Weaver All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN-13 (pbk): 978-1-4302-0554-8 ISBN-10 (pbk): 1-4302-0554-7 Printed and bound in the United States of America (POD) Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc Lead Editor: Steve Anglin Technical Reviewer: Weiqi Gao Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jason Gilmore, Jonathan Hassell, Chris Mills, Matthew Moodie, Jeffrey Pepper, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Edit Manager: Nicole Flores Copy Editor: Damon Larson Assistant Production Director: Kari Brooks-Copony Compositor: Richard Ables Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section Contents About the Authors vii About the Technical Reviewer ix Acknowledgments xi ■CHAPTER Introduction to JavaFX The JavaFX Family JavaFX Mobile JavaFX Script JavaFX Script's Relationship to Java Features and Advantages of JavaFX Script Current Status of JavaFX Script Getting the Most from This Book Summary Resources ■CHAPTER Quick Start Choosing a JavaFX Development Environment Obtaining JavaFXPad Obtaining an IDE Plug-In The Eclipse Plug-In The NetBeans Plug-In Your First JavaFX Application: HelloJFX Understanding the HelloJFX Application Comments 10 The package Declaration 10 import Statements 10 Declarative Code That Defines the User Interface 11 Using the Frame Class 12 Creating String Literals 13 Using the Canvas GUI Widget 13 Drawing Text 13 Defining Fonts 14 Running and Examining the HelloJFXBind Application 15 Structure of a Minimal JavaFX Class 17 The class Declaration 17 iii iv ■CONTENTS attribute Declarations 18 Making an Instance of the Class 18 Declaring and Assigning Variables 18 Understanding Binding 19 Assigning Color Constants to the Text Object 20 Assigning an Array of Values to an Attribute 21 Moving the HelloJFXModel Class into Its Own File 22 Special Instructions for Running This Example with JavaFXPad 24 Summary 26 Resources 27 ■CHAPTER Creating User Interfaces in JavaFX 29 Overview of the Word Search Builder Application 29 Invoking the Application 30 A Tour of the Application 31 The Word Search Builder Application Architecture 36 The Declarative Code and Classes in the wordsearch_jfx.ui Package 37 The Classes in the wordsearch_jfx.model Package 38 Creating the Frame and Menu Structure 39 The Exoskeleton of the Word Search Builder UI 39 Creating Menus 45 Creating a MenuBar Widget 46 Creating Menu Widgets 47 Creating MenuItem Widgets 48 Creating CheckBoxMenuItem Widgets 49 Visually Separating Menu Items 50 Table of Menu-Related Widgets 50 Invoking Java Methods from JavaFX 51 Instantiating the Model, Handler, and View Classes 53 Using Borders 55 Understanding JavaFX Layout Widgets 62 Using the GridPanel Layout Widget 62 Using the Box Layout Widget 64 Using the BorderPanel Layout Widget 65 The Layout Widget Types 66 Creating a ToolBar 69 Using Button Widgets on a Toolbar 71 Summary 77 Resources 78 ■CONTENTS ■CHAPTER Creating JavaFX Classes and Objects 81 ■CHAPTER Testing the Word Search Builder Model 81 Understanding the Structure of a JavaFX Class 87 Understanding Attribute Initializers 87 Introducing Triggers 88 Defining the Body of an Operation 88 Producing Console Output 89 Creating String Expressions 90 Invoking an Operation Located in the Same Class 90 The for Statement 90 Examining the Model Behind the Word Search Grid 94 Understanding JavaFX Data Types 107 Considerations When Declaring an Attribute 108 Considerations When Declaring a var 110 Defining and Using Named Instances (Constants) 111 Creating Operations and Functions 112 Defining the Parameters and Return Type of an Operation 112 Understanding the Effects of the new Operator 114 Creating JavaFX Functions 116 Understanding JavaFX Triggers 116 Using JavaFX Statements and Operators 117 The if/else Statement 118 The while Statement 119 The break Statement 120 Using JavaFX Sequences 127 Sequence Literals 130 Accessing a Specific Element of a Sequence 131 Iterating Over an Sequence 132 Inserting Sequence Elements 132 Querying Sequences 133 Deleting Sequence Elements 134 Zeroing Out a Sequence 135 The JavaFX Statements 136 The JavaFX Operators 137 The Model Behind Each Word Search Grid Cell 139 The Model Behind the Word List Boxes 141 Naming Rules and Conventions for JavaFX Identifiers 143 Summary 144 Resources 145 More UI Fun Including 2D Drawing 147 v vi ■CONTENTS Understanding JavaFX 2D Graphic 147 Drawing and Painting Shapes 149 Transforming Graphics Objects 150 Using the Group Node to Group Shapes Together 150 Canvas Mouse Events 155 Using the var Pseudo-Attribute 157 Creating Custom Graphical Components 158 Extending CompositeNode 162 Working with Images on the Canvas 163 Controlling the Opacity of a Node 164 Animating a Node 165 Using a Custom Component in a Program 165 Examining the WordGridView Custom Graphical Component 166 Using the PopupMenu Widget 175 Creating Custom Widgets 179 Supplying a composeWidget() Operation 181 Creating and Using the ListBox Widget 181 Creating Dialog Boxes 182 Using the JavaFX MessageDialog Class 192 Using the Java Swing JOptionPane Class 192 Using the JavaFX Dialog Class 194 Experiencing the GroupPanel Layout 198 Using the RadioButton Widget 198 More JavaFX UI Components 198 Summary 199 Resources 200 About the Author ■JAMES L (JIM) WEAVER is chief technical officer at Learning Assistant Technologies (http://lat-inc.com/), a technology consulting and software development company He is also president of JMentor (http://jmentor.com/), for which he writes books and provides training and consulting services on the subjects of and JavaFX and the Java™ programming language In his free time, Jim enjoys his family (including two grandchildren), plays guitar, sings in a gospel quartet, and plays racquetball vii ... technologies: JavaFX Mobile and JavaFX Script JavaFX Mobile JavaFX Mobile is essentially a layered software system for mobile devices on which programs developed in JavaFX Script can execute JavaFX Script. .. Microsystems JavaFX web site: This site describes the JavaFX product family The URL is www.sun.com/javafx • The JavaFX announcement at JavaOne: This page contains Sun Microsystems’ original JavaFX announcement... having declarative scripting, JavaFX Script is a fully objectoriented language, complete with methods (called operations and functions in JavaFX) and attributes JavaFX Script, like Java, is statically