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 section explains how to compile and run a Swing application from the command line. 本节介绍如何从命令行编译和运行Swing应用程序。For information on compiling and running a Swing application using NetBeans IDE, see Running Tutorial Examples in NetBeans IDE. 有关使用NetBeans IDE编译和运行Swing应用程序的信息,请参阅在NetBeans IDE中运行教程示例。The compilation instructions work for all Swing programs applets, as well as applications. 编译指令适用于所有Swing程序小程序以及应用程序。Here are the steps you need to follow:以下是您需要遵循的步骤:
You can download the latest release of the JDK for free from http://www.oracle.com/technetwork/java/javase/downloads/index.html.您可以从以下站点免费下载JDK的最新版本:http://www.oracle.com/technetwork/java/javase/downloads/index.html。
You can use a simple program we provide, called HelloWorldSwing, that brings up the GUI shown in the figure below. 您可以使用我们提供的一个名为HelloWorldSwing的简单程序,该程序将显示下图所示的GUI。The program is in a single file, 该程序位于单个文件HelloWorldSwing.java
. HelloWorldSwing.java
中。When you save this file, you must match the spelling and capitalization of its name exactly.保存此文件时,必须完全匹配其名称的拼写和大小写。
The 与我们所有的Swing教程示例一样,HelloWorldSwing.java
example, like all of our Swing tutorial examples, is created inside a package. HelloWorldSwing.java
示例是在一个包中创建的。If you look at the source code, you see the following line at the beginning of the file:如果查看源代码,您会在文件开头看到以下行:
package start;
This means you must put the 这意味着您必须将HelloWorldSwing.java
file inside of a start
directory. HelloWorldSwing.java
文件放在start
目录中。You compile and run the example from the directory above the 您可以从start
directory. start
目录上方的目录编译并运行示例。The tutorial examples from the Using Swing Components lesson are inside of a “使用Swing组件”课程中的教程示例位于components
package and the examples from the Writing Event Listeners lesson are inside a events
package, and so on. components
包内,“编写事件侦听器”课程中的示例位于events
包内,依此类推。For more information, you might want to see the 有关详细信息,您可能希望查看Packages
lesson.Packages
课程。
Your next step is to compile the program. 下一步是编译程序。To compile the example, from the directory above the 要编译示例,请从HelloWorldSwing.java
file:HelloWorldSwing.java
文件上方的目录:
javac start/HelloWorldSwing.java
If you prefer, you may compile the example from within the 如果愿意,可以从start
directory:start
目录中编译示例:
javac HelloWorldSwing.java
but you must remember to leave the 但是您必须记住离开start
directory to execute the program.start
目录来执行程序。
If you are unable to compile, make sure you are using the compiler in a recent release of the Java platform. 如果无法编译,请确保在Java平台的最新版本中使用编译器。You can verify the version of your compiler or Java Runtime Environment (JRE) using these commands您可以使用以下命令验证编译器或Java运行时环境(JRE)的版本
javac -version java -version
Once you've updated your JDK, you should be able to use the programs in this trail without changes. 一旦您更新了JDK,您应该能够在不做任何更改的情况下使用本教程中的程序。Another common mistake is installing the JRE and not the full Java Development Kit (JDK) needed to compile these programs. 另一个常见错误是安装JRE,而不是编译这些程序所需的完整Java开发工具包(JDK)。Refer to the Getting Started trail to help you solve any compiling problems you encounter. 请参阅入门教程,以帮助您解决遇到的任何编译问题。Another resource is the Troubleshooting Guide for Java™ SE 6 Desktop Technologies.另一个资源是Java™SE6桌面技术的疑难解答指南。
After you compile the program successfully, you can run it. 成功编译程序后,可以运行它。From the directory above the 从start
directory:start
目录上方的目录:
java start.HelloWorldSwing