GroovyFX介绍

[复制链接]
查看11 | 回复5 | 2014-2-19 11:55:14 | 显示全部楼层 |阅读模式
GroovyFX brings together two of my favorite things: Groovy and JavaFX. The main GroovyFX Project page describes GroovyFX as "[providing] a Groovy binding for JavaFX 2.0." GroovyFX is further described on that page:
1. GroovyFX is an API that makes working with JavaFX in Groovy much simpler and more natural.
2. GroovyFX is focused on exploiting the power of the Groovy Builder pattern to make JavaFX development easier and more concise than what is possible in Java.
3. GroovyFX also leverages Groovy's powerful DSL features and AST transformations to eliminate boilerplate, making GroovyFX code easier to write and, just as importantly, easier to read.
The just referenced main GroovyFX page includes a "Hello World" example. In this post, I look at an even simpler "Hello World" example using GroovyFX. After that, I look at a slightly more involved example of using GroovyFX to render a Pie Chart.
Both of these are examples I intend to show at my RMOUG Training Days 2013 presentation ("Charting Oracle Database Data with JavaFX and Groovy") this coming week.
A bare-bones GroovyFX Hello World! example is shown in the next code listing.


回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
01.import groovyx.javafx.GroovyFX
02.import groovyx.javafx.SceneGraphBuilder
03.import javafx.stage.StageStyle
04.import javafx.stage.Stage
05.
06.GroovyFX.start
07.{
08.stage(title: 'RMOUG Training Days 2013',
09.width: 300, height: 100,
10.show: true)
11.{
12.scene
13.{
14.stackPane
15.{
16.text('Hello GroovyFX!', x: 50, y: 40)
17.}
18.}
19.}
20.}
复制代码Running the above script leads to the following output:


rmougTd2013HelloWorldJavaFX.png (8.45 KB, 下载次数: 6)
下载附件
2013-2-21 09:16 上传

回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
The code and screen snapshot show how the concise text of GroovyFX makes it easy in just a few lines of code to specify a fully functioning JavaFX graphical application.
The next code listing shows a slightly more involved examples that generates a JavaFX Pie Chart. The database access code is not shown here, but it is easily accomplished with JDBC or Groovy SQL.01.import rmoug.td2013.dustin.examples.ChartMaker
02.import rmoug.td2013.dustin.examples.DbAccess
03.import groovyx.javafx.GroovyFX
04.import groovyx.javafx.SceneGraphBuilder
05.import javafx.stage.StageStyle
06.import javafx.stage.Stage
07.
08.def databaseAccess = DbAccess.newInstance()
09.
10.GroovyFX.start
11.{
12.stage(title: 'Employees Per Department',
13.width: 800, height: 500,
14.show: true)
15.{
16.scene
17.{
18.stackPane
19.{
20.pieChart(title: 'Number of Employees per Department',
21.data: ChartMaker.createPieChartDataForNumberEmployeesPerDepartment(
22.databaseAccess.getNumberOfEmployeesPerDepartmentName()))
23.}
24.}
25.}
26.}
复制代码
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
The above GroovyFX code leads to the following screen snapshot.


rmougTd2013PieChartExample.png (94.78 KB, 下载次数: 4)
下载附件
2013-2-21 09:16 上传

The simple GroovyFX code shown above combines Groovy with JavaFX to render a pie chart representation of the number of employees per department in the Oracle 'hr' sample schema.
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
The next code sample indicates the roughly equivalent source code for a JavaFX application that does not use GroovyFX.01.package rmoug.td2013.dustin.examples;
02.
03.import javafx.application.Application;
04.import javafx.scene.Scene;
05.import javafx.scene.chart.PieChart;
06.import javafx.scene.layout.StackPane;
07.import javafx.stage.Stage;
08.
09.public class EmployeesPerDepartmentPieChart extends Application
10.{
11.final DbAccess databaseAccess = DbAccess.newInstance();
12.
13.@Override
14.public void start(final Stage stage) throws Exception
15.{
16.final PieChart pieChart =
17.new PieChart(
18.ChartMaker.createPieChartDataForNumberEmployeesPerDepartment(
19.this.databaseAccess.getNumberOfEmployeesPerDepartmentName()));
20.pieChart.setTitle("Number of Employees per Department");
21.stage.setTitle("Employees Per Department");
22.final StackPane root = new StackPane();
23.root.getChildren().add(pieChart);
24.stage.setScene(new Scene(root, 800 ,500));
25.stage.show();
26.}
27.
28.public static void main(final String[] arguments)
29.{
30.launch(arguments);
31.}
32.}
复制代码The code for the direct JavaFX example can be compared to the GroovyFX example to see how the GroovyFX syntax is more concise (as is expected for something based on Groovy) and arguably more readable than the straight JavaFX code (though I maintain the JavaFX code is fairly readable in its own right). Comparing the two code samples also helps to see how GroovyFX uses property names well-known to users of the JavaFX API.
回复

使用道具 举报

千问 | 2014-2-19 11:55:14 | 显示全部楼层
over.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行