blob: 5dace377b948ed75354b1ca936a349419e51fc1a [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= Groovy 简介
:jbake-type: tutorial
:jbake-tags: tutorials
:jbake-status: published
:icons: font
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:description: Groovy 简介 - Apache NetBeans
:keywords: Apache NetBeans, Tutorials, Groovy 简介
本文档将指导您在 NetBeans IDE 中使用 link:http://groovy.codehaus.org/[+Groovy+]。您将创建一个 Java 应用程序和一个 JFrame,然后从 Groovy 文件中检索一条简单的消息。
image::images/netbeans-stamp-80-74-73.png[title="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0"]
*要学习本教程,您需要具备以下软件和资源。*
|===
|软件或资源 |要求的版本
|link:https://netbeans.org/downloads/index.html[+NetBeans IDE+] |7.2、7.3、7.4、8.0、Java 包
|link:http://www.oracle.com/technetwork/java/javase/downloads/index.html[+Java 开发工具包 (JDK)+] |版本 7 或 8
|Groovy 插件 |-
|===
== 激活 Groovy 插件
安装 Java 版本的 IDE 时,默认情况下禁用对 Groovy 的支持。要在 IDE 中使用 Groovy,您首先需要在插件管理器中激活 Groovy 插件。
1. 从主菜单中选择 *Tools*(工具)> *Plugins*(插件)以打开插件管理器。
2. 在插件管理器中单击 *Installed*(已安装)标签。
3. 选择 "Show Details"(显示详细信息),然后在插件列表中查找 *Groovy and Grails*(Groovy Grails)插件。
或者,您也可以在搜索字段中键入 `groovy` 以过滤插件列表。
[start=4]
. 选中 "Groovy and Grails"Groovy Grails)插件对应的复选框,然后单击 "Activate"(激活)。
image::images/groovy-plugin.png[title="在插件管理器中选定的 Groovy 插件"]
[start=5]
. "NetBeans IDE Installer"NetBeans IDE 安装程序)窗口中单击 "Activate"(激活)以激活 Groovy 插件。
[start=6]
. 在安装完成之后,在 "NetBeans IDE Installer"NetBeans IDE 安装程序)窗口中单击 "Finish"(完成),然后在插件管理器中单击 "Close"(关闭)。
== 创建应用程序
在此部分,您将创建一个新的 Java 应用程序。
1. 从主菜单中选择 "File"(文件)> "New Project"(新建项目)(Ctrl-Shift-N 组合键;在 Mac 上为 ⌘-Shift-N 组合键),以打开新建项目向导。
2. "Java" 类别中选择 "Java Application"Java 应用程序)。单击 "Next"(下一步)。
3. 键入 *GroovyJavaDemo* 作为项目名称,并在 "Project Location"(项目位置)字段中指定要创建应用程序的文件夹。
4. 取消选中 "Create Main Class"(创建主类)复选框。单击 "Finish"(完成)。
单击 "Finish"(完成)后,IDE 将创建项目并在 "Projects"(项目)窗口中显示项目节点。
== 创建 Java 类和 Groovy 文件
在此部分,您将创建一个 JFrame 和一个 Groovy 类。
1. "Projects"(项目)窗口中右键单击项目节点,然后选择 "New"(新建)> "Other"(其他)以打开 "New File"(新建文件)对话框。
2. "Swing GUI Forms"Swing GUI 窗体)类别中选择 "JFrame Form"JFrame 窗体)。单击 "Next"(下一步)。
image::images/groovy-newjframe.png[title=""New File"(新建文件)向导中的 JFrame 窗体模板"]
[start=3]
. 键入 *DisplayJFrame* 作为类名。
[start=4]
. 键入 *org.demo* 作为包。单击 "Finish"(完成)。
image::images/groovy-newjframe2.png[title=""New JFrame Form"(新建 JFrame 窗体)向导中的 "Name and Location"(名称和位置)面板"]
单击 "Finish"(完成)后,IDE 将创建 JFrame 窗体并在编辑器中打开文件。
[start=5]
. "Projects"(项目)窗口中右键单击项目节点,然后选择 "New"(新建)> "Other"(其他)以打开 "New File"(新建文件)对话框。
[start=6]
. "Groovy" 类别中选择 "Groovy Class"Groovy 类)。单击 "Next"(下一步)。
image::images/groovy-newgroovyclass.png[title=""New File"(新建文件)向导中的 Groovy 类模板"]
[start=7]
. 键入 *GreetingProvider* 作为类名。
[start=8]
. "Package"(包)下拉列表中选择 ``org.demo`` 包。单击“完成”。
单击 "Finish"(完成)后,IDE 将创建 Groovy 文件并在编辑器中打开该文件。
如果在 "Projects"(项目)窗口中展开项目节点,您可以看到所创建的两个文件位于 "Source Packages"(源包)节点下。
== Java 中调用 Groovy
在此部分,您将通过代码实现 Groovy 文件与 Java 类的交互。
1. 在编辑器中打开 ``GreetingProvider.groovy`` (如果尚未打开)。
2. 通过添加以下代码(粗体),在类定义内定义问候语变量。保存所做的更改。
[source,java]
----
class GreetingProvider {
*def greeting = "Hello from Groovy"*
}
----
[start=3]
. 在编辑器中打开 ``DisplayJFrame.java`` (如果尚未打开),然后单击 "Design"(设计)标签。
[start=4]
. 打开 "Palette"(组件面板)(从主菜单中选择 "Window"(窗口)> "Palette"(组件面板)),然后将 "Text Field"(文本字段)元素 ( ``jTextField`` ) "Palette"(组件面板)拖放到 JFrame
image::images/groovy-jtextfield.png[title="在编辑器 "Design"(设计)视图中打开的 DesignJFrame 中的 JTextfield"]
[start=5]
. 在编辑器中单击 "Source"(源)标签,然后在类主体顶部添加以下代码以实例化 Groovy 类并在构造函数中调用 Groovy 类中的 ``getGreeting()`` 方法。
[source,java]
----
public class DisplayJFrameForm extends javax.swing.JFrame {*
GreetingProvider provider = new GreetingProvider();*
public DisplayJFrame() {
initComponents();
*String greeting = provider.getGreeting().toString();
jTextField1.setText(greeting);*
}
----
您可以在 Java 类中使用代码完成来查找 Groovy 类中需要的方法。
image::images/groovy-codecompletion.png[title="编辑器中的代码完成功能"]
[start=6]
. "Projects"(项目)窗口中右键单击项目节点,然后选择 "Run"(运行)。
选择 "Run"(运行)后,IDE 将编译并启动应用程序。
image::images/groovy-runproject.png[title="在文本字段中显示 Groovy 类中文本的应用程序的窗口"]
在应用程序的窗口中,您可以看到 Groovy 类中的文本显示在文本字段中。
现在,您已经了解了如何创建与 Groovy 交互的基本 Java 应用程序。
link:/about/contact_form.html?to=3&subject=Feedback:%20NetBeans%20IDE%20Groovy%20Quick%20Start[+发送有关此教程的反馈意见+]
== 另请参见
NetBeans IDE 还支持 Grails Web 框架,该框架使用 Groovy 语言进行 Java Web 开发。要了解如何在 NetBeans IDE 中使用 Grails 框架,请参见 link:../web/grails-quickstart.html[+Grails 框架简介+]。