Documentation

The Java™ Tutorials
Hide TOC

Running Tutorial Examples in NetBeans IDE在NetBeans IDE中运行教程示例

The examples in the Swing Tutorial and Deployment Tutorial are also provided as NetBeans projects that are ready to be opened and run inside of NetBeans IDE, a free integrated development environment that you can use to simplify application development.Swing教程和部署教程中的示例也作为NetBeans项目提供,这些项目可以在NetBeans IDE中打开并运行,NetBeans IDE是一个免费的集成开发环境,可以用来简化应用程序开发。

This page provides information on the following tasks:此页面提供有关以下任务的信息:

Downloading, Installing, and Launching NetBeans IDE下载、安装和启动NetBeans IDE

The easiest way to get NetBeans IDE is to download the NetBeans IDE installer for your operating system from the NetBeans download page.获取NetBeans IDE的最简单方法是从NetBeans下载页面下载操作系统的NetBeans IDE安装程序。

You can then run the installer and complete a short sequence of steps in the installation wizard to set up the IDE. 然后,您可以运行安装程序并在安装向导中完成一小段步骤来设置IDE。You can find more details on the installation process from the installation instructions that are linked to from the download page.您可以从下载页面链接到的安装说明中找到有关安装过程的更多详细信息。

You can then launch the IDE in one of the following ways:然后,您可以通过以下方式之一启动IDE:

Opening a Project for an Example为示例打开项目

Opening a project in NetBeans IDE consists of the following steps:在NetBeans IDE中打开项目包括以下步骤:

ButtonDemoProject

Once opened, the project appears in the IDE's Projects window. 打开后,项目将显示在IDE的“项目”窗口中。Expand the nodes of the project to show the project's structure. 展开项目的节点以显示项目的结构。The project includes the following main subnodes:该项目包括以下主要子节点:

To open a file, double-click the file's node in the Projects window. 要打开文件,请在“项目”窗口中双击该文件的节点。When opened, a file is displayed in the Source Editor, to the right of the Projects window.打开时,一个文件将显示在“项目”窗口右侧的源代码编辑器中。

The Projects window represents the "logical" structure of the project, which does not correspond to the layout of the files on your system. “项目”窗口表示项目的“逻辑”结构,它与系统上的文件布局不对应。This logical structure highlights the parts of the project that you need to access most often. 此逻辑结构突出显示了项目中最需要访问的部分。The Projects window does not display compiled .class files. “项目”窗口不显示已编译的.class文件。If you want to see how the entire project is physically represented on disk, click the Files tab.如果要查看整个项目在磁盘上的物理表示方式,请单击“文件”选项卡。

Compiling and Running a Project编译和运行项目

For applications that are accessed through a browser, add the Codebase and Permissions attributes in the JAR file manifest before you compile the project. 对于通过浏览器访问的应用程序,在编译项目之前,在JAR文件清单中添加CodebasePermissions属性。In the Files tab, expand the project folder to see the subfolders and files. 在“文件”选项卡中,展开项目文件夹以查看子文件夹和文件。Double-click manifest.mf to open the manifest file, and set the Codebase attribute to the location from which the application is loaded. 双击manifest.mf打开manifest文件,并将Codebase属性设置为加载应用程序的位置。Set the Permissions attribute to the permissions level that the application requires. Permissions属性设置为应用程序所需的权限级别。See JAR File Manifest Attributes for Security in the Java Platform, Standard Edition Deployment Guide for more information.有关更多信息,请参阅Java平台中的用于安全性的JAR文件清单属性,标准版部署指南。

To compile your whole project, right-click the project's node in the Projects window and choose Build Project.要编译整个项目,请在“项目”窗口中右键单击该项目的节点,然后选择“生成项目”。

The Output window opens and displays output on the compilation progress.“输出”窗口将打开并显示编译进度的输出。

If the build output concludes with the statement BUILD FAILED, you probably have a syntax error in your code. 如果生成输出以语句BUILD FAILED结束,则代码中可能存在语法错误。Errors are reported in the Output window as hyperlinked text. 错误在输出窗口中作为超链接文本报告。You double-click such a hyperlink to navigate to the source of an error. 双击此类超链接可导航到错误源。You can then fix the error and once again choose Build Project.然后,您可以修复错误并再次选择Build Project。

When you build the project, bytecode .class files are generated. 构建项目时,将生成字节码.class文件。You can see where the new files are generated by opening the Files window and expanding the folder for the project.通过打开“文件”窗口并展开项目的文件夹,可以查看新文件生成的位置。

To run your project, right-click the project's node in the Projects window and choose Run Project.要运行项目,请在“项目”窗口中右键单击项目的节点,然后选择“运行项目”。


Note: You can also use the Build Main Project and Run Main Project commands in the main menu. 还可以使用主菜单中的“生成主项目”和“运行主项目”命令。However, these commands work only work on the project that is currently designated as the "main" project. 但是,这些命令仅适用于当前指定为“主”项目的项目。You can make a project the main project by right-clicking its node in the Projects window and choosing Set Main Project. 通过在“项目”窗口中右键单击项目节点并选择“设置主项目”,可以使项目成为主项目。

For more tips on using NetBeans IDE with the Java Tutorial, see "HelloWorld!" for the NetBeans IDE.有关在Java教程中使用NetBeans IDE的更多提示,请参阅NetBeans IDE的“HelloWorld!”