The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
This lesson provides an introduction to Graphical User Interface (GUI) programming with Swing and the NetBeans IDE. 本课程介绍使用Swing和NetBeans IDE进行图形用户界面(GUI)编程。As you learned in the "Hello World!" lesson, the NetBeans IDE is a free, open-source, cross-platform integrated development environment with built-in support for the Java programming language. 正如您在"Hello World!"课程中所学到的,NetBeans IDE是一个免费、开源、跨平台的集成开发环境,内置了对Java编程语言的支持。It offers many advantages over coding with a text editor; we recommend its use whenever possible. 与使用文本编辑器编码相比,它有许多优点;我们建议尽可能使用它。If you have not yet read the above lesson, please take a moment to do so now. 如果您尚未阅读上述课程,请现在花点时间阅读。It provides valuable information about downloading and installing the JDK and NetBeans IDE.它提供了有关下载和安装JDK和NetBeans IDE的宝贵信息。
The goal of this lesson is to introduce the Swing API by designing a simple application that converts temperature from Celsius to Fahrenheit. 本课程的目标是通过设计一个将温度从摄氏度转换为华氏度的简单应用程序来介绍Swing API。Its GUI will be basic, focusing on only a subset of the available Swing components. 它的GUI将是基本的,只关注可用Swing组件的一个子集。We will use the NetBeans IDE GUI builder, which makes user interface creation a simple matter of drag and drop. 我们将使用NetBeans IDE GUI构建器,它使用户界面的创建成为一个简单的拖放操作。Its automatic code generation feature simplifies the GUI development process, letting you focus on the application logic instead of the underlying infrastructure.它的自动代码生成功能简化了GUI开发过程,使您能够专注于应用程序逻辑,而不是底层基础设施。
Because this lesson is a step-by-step checklist of specific actions to take, we recommend that you run the NetBeans IDE and perform each step as you read along. 因为本课程是一个具体操作的分步清单,我们建议您运行NetBeans IDE并在阅读过程中执行每一步。This will be the quickest and easiest way to begin programming with Swing. 这将是使用Swing开始编程的最快、最简单的方法。If you are unable to do so, simply reading along should still be useful, since each step is illustrated with screenshots.如果你不能做到这一点,简单的阅读仍然是有用的,因为每个步骤都有截图。
If you prefer the traditional approach of programming each component manually (without the assistance of an IDE), think of this lesson as an entry point into the lower-level discussions already provided elsewhere in the tutorial. 如果您更喜欢手动编程每个组件的传统方法(无需IDE的帮助),请将本课视为本教程其他部分已提供的较低级别讨论的切入点。Hyperlinks in each discussion will take you to related lessons, should you wish to learn such lower-level details.如果您希望了解此类较低级别的详细信息,每次讨论中的超链接将带您进入相关课程。
The finished GUI for this application will look as follows:此应用程序的成品GUI如下所示:
The CelsiusConverter Application.CelsiusConverter应用程序。
Click the Launch button to run CelsiusConverter using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web Start运行CelsiusConverter(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
From an end-user's perspective, usage is simple: enter a temperature (in Celsius) into the text box, click the "Convert" button, and watch the converted temperature (in Fahrenheit) appear on screen. 从最终用户的角度来看,使用方法很简单:在文本框中输入温度(摄氏度),单击“转换”按钮,然后观察转换后的温度(华氏度)显示在屏幕上。The minimize, maximize, and close buttons will behave as expected, and the application will also have a title that appears along the top of the window.最小化、最大化和关闭按钮将按预期运行,应用程序也将在窗口顶部显示标题。
From a programmer's perspective, we will write the application in two main stages. 从程序员的角度来看,我们将分两个主要阶段编写应用程序。First, we will populate the GUI with the various Swing components and arrange them as shown above. 首先,我们将用各种Swing组件填充GUI,并按照上面所示排列它们。Then, we will add the application logic, so that the program actually performs a conversion when the user presses the "Convert" button.然后,我们将添加应用程序逻辑,以便当用户按下“转换”按钮时,程序实际执行转换。
If you are interested in using JavaFX to create your GUI, see the JavaFX Documentation and JavaFX - NetBeans Wiki.如果您对使用JavaFX创建GUI感兴趣,请参阅JavaFX文档和JavaFX-NetBeans Wiki。