Documentation

The Java™ Tutorials
Hide TOC
Compiling and Running Swing Programs编译和运行Swing程序
Trail: Creating a GUI With Swing
Lesson: Getting Started with Swing

Compiling and Running Swing Programs编译和运行Swing程序

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:以下是您需要遵循的步骤:

Install the Latest Release of the Java SE Platform安装最新版本的Java SE平台

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

Create a Program That Uses Swing Components创建一个使用Swing组件的程序

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 HelloWorldSwing.java example, like all of our Swing tutorial examples, is created inside a package. 与我们所有的Swing教程示例一样,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 components package and the examples from the Writing Event Listeners lesson are inside a events package, and so on. “使用Swing组件”课程中的教程示例位于components包内,“编写事件侦听器”课程中的示例位于events包内,依此类推。For more information, you might want to see the Packages lesson.有关详细信息,您可能希望查看Packages课程。

Screen shot of HelloWorldSwing application

Compile the Program编译程序

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桌面技术的疑难解答指南

Run the Program运行程序

After you compile the program successfully, you can run it. 成功编译程序后,可以运行它。From the directory above the start directory:start目录上方的目录:

java start.HelloWorldSwing

Previous page: About the JFC and Swing
Next page: Learning Swing with the NetBeans IDE