An Introduction To Java Programming

Transcription

Supplement H.1: JBuilder X TutorialFor Introduction to Java Programming, 5EBy Y. Daniel LiangThis supplement covers the following topics: Getting Started with JBuilderCreating a ProjectCreating, Compiling, and Running a Java ProgramDebugging in JBuilderGetting Help in JBuilderCreating and Testing Java AppletsNOTE: To use this supplement with the text,cover Sections 1, 2, and 3 in this supplementafter Chapter 1 in the text, cover Sections 4and 5 in this supplement after Chapter 2 in thetext, and cover Section 6 in this supplement atthe beginning of Chapter 14 in the text.0 IntroductionThis tutorial is for students who are currently taking aJava course using JBuilder with Introduction to JavaProgramming, 5E.You can use the JDK command line utility to write Javaprograms. The JDK command line utility consists of a set ofseparate programs, such as compiler and interpreter, each ofwhich is invoked from a command line. Besides the JDKcommand line utility, there are more than a dozen Javadevelopment tools on the market today, including BorlandJBuilder, NetBeans, Sun ONE Studio (a commercial version ofNetBeans), Eclipse, and WebGain Visual Café. These toolssupport an integrated development environment (IDE) forrapidly developing Java programs. Editing, compiling,building, debugging, and online help are integrated in onegraphical user interface. Using these tools effectively willgreatly increase your programming productivity.This brief tutorial will help you to become familiarwith JBuilder. Specifically, you will learn how tocreate projects, create programs, compile, and runprograms.8 Copyright Y. Daniel Liang, 2005

NOTE: JBuilder can run on any platform with aJava Virtual Machine. The screen shots in thetutorial are taken from Windows using JBuilder X.You can download JBuilder X Foundation fromwww.borland.com.NOTE: JBuilder X is bundled with JDK 1.4. To useJDK 1.5 in JBuilder X, (1) download and installJDK 1.5. JDK 5.0 can be downloaded fromhttp://java.sun.com/j2se/1.5.0/download.jsp. SeeSupplement A, “Installing JDK 5.0,” on how toinstall JDK 5.0; (2) See Supplement H.3 on how toconfigure and set JDK 1.5 in JBuilder.1 Getting Started with JBuilderAssume you have successfully installed JBuilder on yourmachine. Start JBuilder from Windows, Solaris, Linux, orMac. The main JBuilder user interface appears, as shown inFigure 1.1. If you don't see the Welcome project, chooseWelcome Project (Sample) from the Help menu.NOTE: The screen shots in this book are fromJBuilder X Enterprise. If you use JBuilder XFoundation, your screen will look almostidentical.Figure 1.19 Copyright Y. Daniel Liang, 2005

The JBuilder user interface is a single window thatperforms functions for editing, compiling, debugging,and running programs.The JBuilder user interface presents a single window, calledAppBrowser, for managing Java projects, browsing files,compiling, running, and debugging programs. The AppBrowserwindow primarily consists of the main menu, main toolbar,status bar, project pane, structure pane, and content pane.1.1 The Main MenuThe main menu is similar to that of other Windowsapplications and provides most of the commands you need touse JBuilder, including those for creating, editing,compiling, running, and debugging programs. The menu itemsare enabled and disabled in response to the current context.1.2 The ToolbarThe toolbar provides buttons for several frequently usedcommands on the menu bar. Clicking a toolbar is faster thanusing the menu bar. For some commands, you also can usefunction keys or keyboard shortcuts. For example, you cansave a file in three ways: Select File, Save from the menu bar. Click the "save" toolbar button. Use the keyboard shortcut Ctrl S.TIP: You can display a label, known as ToolTip,for a button by pointing the mouse to the buttonwithout clicking.1.3 The Status BarThe status bar displays a message that alerts the user tothe operation status, such as file saved for the Save filecommand and compilation successful for the Compilationcommand.1.4 The Project PaneThe project pane (known as the navigation pane in JBuilder3) displays the contents of one or more projects opened inthe AppBrowser. It consists of the following items, as shownin Figure 1.2.10 Copyright Y. Daniel Liang, 2005

Figure 1.2The project pane manages JBuilder projects. ,A small toolbar with four buttons (Close ProjectAdd To Project, Remove From Project, and Refresh). A drop-down list of all opened projects. A tree view of all the files that make up the activeproject.The project pane shows a list of one or more files. Theproject (.jpx) file appears first. Attached to it is a listof the files in the project. The list can include .java,.html, text, or image files. You select a file in theproject pane by clicking it. The content pane and thestructure pane display information about the selected file.As you select different files in the project pane, each onewill be represented in the content and structure panes.The project pane shown in Figure 1.2 contains three files.The Add button is used to add new files to the project, andthe Remove button to remove files from the project. Forexample, you can remove Welcome.html by selecting the filein the project pane and clicking the Remove button. You canthen add the file back to the project as follows:1.Click the Add button to display the Opendialog box shown in Figure 1.3.2.Open Welcome.html. You will see Welcome.htmldisplayed in the project pane.11 Copyright Y. Daniel Liang, 2005

Figure 1.3The Open dialog box enables you to open an existingfile.TIP: You can select multiple files by clickingthe files with the CTRL key pressed, or selectconsecutive files with the SHIFT key pressed.1.5 The Content PaneThe content pane displays all the opened files as a set oftabs. To open a file in the content pane, double-click it inthe project pane. The content pane displays the detailedcontent of the selected file. The editor or viewer used isdetermined by the file's extension. If you click theWelcomeApp.java file in the project pane, for example, youwill see six tabs (Source, Design, Bean, UML, Doc, andHistory) at the bottom of the content pane (see Figure 1.1).If you select the Source tab, you will see the JBuilder Javasource code editor. This is a full-featured, syntaxhighlighted programming editor.If you select Welcome.html in the project pane, you will seethe content pane become an HTML browser, as shown in Figure1.4. If you choose the Source tab, you can view and edit theHTML code in the content pane, as shown in Figure 1.5.12 Copyright Y. Daniel Liang, 2005

Figure 1.4JBuilder renders HTML files in the content pane.Figure 1.5You can edit HTML files in the content pane.13 Copyright Y. Daniel Liang, 2005

1.6 The Structure PaneThe structure pane displays the structural information aboutthe files you selected in the project pane. All the itemsdisplayed in the structure pane are in the form of ahierarchical indexed list. The expand symbol in front of anitem indicates that it contains subitems. You can see thesubitems by clicking on the expand symbol.You can also use the structure pane as a quick navigationaltool to the various structural elements in the file. If youselect the WelcomeFrame.java file, for example, you will seeclasses, variables, and methods in the structure pane. Ifyou then click on any of those elements in the structurepane, the content pane will move to and highlight it in thesource code.If you click on the jMenuFile item in the structure pane, asshown in Figure 1.6, the content pane moves to andhighlights the statement that defines the jMenuFile datafield. This provides a much faster way to browse and findthe elements of a file than scrolling through it.Figure 1.614 Copyright Y. Daniel Liang, 2005

You can cruise through the source code from thestructure pane.2 Creating a ProjectA project is like a holder that ties all the files together.The information about each JBuilder project is stored in aproject file with a .jpx file extension. (Prior to JBuilder7, the project file extension was either .jpx or .jpr. The.jpr project files are still compatible in JBuilder X.) Theproject file contains a list of all the files and projectsettings and properties. JBuilder uses this information toload and save all the files in the project and compile andrun the programs. To create and run a program, you have tofirst create a project.To avoid frustrating mistakes, it is important to createprojects in a consistent and uniform way for all yourprograms. I recommend that new users create a project asfollows:1.Choose File, New Project to bring up the Projectwizard dialog box, as shown in Figure 2.1.Figure 2.1The Project wizard dialog box enables you to specifythe project file with other optional information.15 Copyright Y. Daniel Liang, 2005

2.Type tutorial in the Name field and c:/smith in theDirectory field. Check the Generate project notesfile option box. Click Next to display Project WizardStep 2 of 3, as shown in Figure 2.2.Figure 2.2Project Wizard Step 2 of 3 enables you to modifyproject settings.3.Type c:/smith in the Output path field, c:/smith/bakin the Backup path field, and c:/smith in the Workingdirectory field. In the Source tab, change theDefault path and Test path to c:/smith. To do so,click the Edit button to display the Select Directorydialog box, as shown in Figure 2.3. Type c:/smith inthe Directory field. If the directory does not exist,you will see the No Such Directory dialog box, asshown in Figure 2.4. Click Yes to return to ProjectWizard - Step 2 of 3.16 Copyright Y. Daniel Liang, 2005

Figure 2.3You can select or specify a directory in the SelectDirectory dialog box.Figure 2.4You can select or specify a directory in the SelectDirectory dialog box.4.Click Next in Project Wizard Step 2 of 3 to displayProject Wizard – Step 3 of 3, as shown in Figure 2.5.Fill in the title, author, company, and descriptionfields. These optional fields provide a descriptionfor the project. (Uncheck Enable source packagediscovery and compilation to shorten the contentsdisplayed in the project pane)5.Click Finish. The new project is displayed in theproject pane, as shown in Figure 2.6. The Projectwizard created the project file (tutorial.jpx) and anHTML file (tutorial.html), and placed them inc:\smith. The project file stores the information17 Copyright Y. Daniel Liang, 2005

about the project, and the HTML file is used todescribe the project.Figure 2.5Project Wizard Step 3 of 3 collects optionalinformation for the project.18 Copyright Y. Daniel Liang, 2005

Figure 2.6A new project is created with the .jpx file and .htmlfile.NOTE: JBuilder automatically generates manybackup files. I use bak as the root directoryfor all these backup files so that they can beeasily located and removed.IMPORTANT CAUTION: Creating a project is apreliminary step before developing Javaprograms. Creating projects incorrectly is acommon problem for new JBuilder users, and canlead to frustrating mistakes. To avoid these,you may create your project exactly as shown inthis section and change smith to your name, orfollow the instructions from your instructors.3 Creating, Compiling, and Running a Java ProgramAfter you created a project, you can create programs in theproject.3.1 Creating a Java Program19 Copyright Y. Daniel Liang, 2005

There are many ways to create a Java program in JBuilder.This tutorial will show you how to use various wizards tocreate certain types of Java programs. In this section, youwill learn how to create Java programs using the Classwizard.The following are the steps in creating a Java program:1. Open the tutorial.jpx project if it is not in theproject pane. To open it, choose File, Reopen to displaya submenu consisting of the most recently openedprojects and files, as shown in Figure 3.1. Select theproject if it is in the menu. Otherwise, choose File,Open to locate and open tutorial.jpx. The project fileis the one with the ( )icon.Figure 3.1Recently used projects can be reopened by choosingFile, Reopen.2.Choose File, New Class to display the Classwizard, as shown in Figure 3.2.20 Copyright Y. Daniel Liang, 2005

Figure 3.2You can use the Class wizard to create a template for anew class.3.In the Class wizard, type Welcome in the Class namefield, leave the Package field blank, and check theoptions Public, Generate main method, and Generateheader comments in the Options section, as shown inFigure 3.2. Click OK to generate Welcome.java, asshown in Figure 3.3.21 Copyright Y. Daniel Liang, 2005

Figure 3.3The program Welcome.java is generated by the Classwizard.4.Add the following line in the main method:System.out.println("Welcome to Java");5.Select File, Save All to save all your work. Youshould see a confirmation message in the status barindicating that the files are saved.NOTE: As you type, the code completionassistance may automatically come up to give yousuggestions for completing the code. Forinstance, when you type a dot (.) after Systemand pause for a second, JBuilder displays apopup menu with hints for completing the code,as shown in Figure 3.4. You can then select fromthe menu to complete the code.22 Copyright Y. Daniel Liang, 2005

Figure 3.4The Code Insight popup menu is automatically displayedto help you complete the code.CAUTION: Java source programs are casesensitive. It would be wrong, for example, toreplace main in the program with Main. Programfile names are case-sensitive on UNIX andgenerally not case-sensitive on PCs, but filenames are case-sensitive in JBuilder.TIP: The public class name must match the filename. To change the file name, right-click thefile in the project pane to display the contextmenu. Choose Rename in the context menu tochange the file name, as shown in Figure 3.5.You can also delete the file from the contextmenu.23 Copyright Y. Daniel Liang, 2005

Figure 3.5The context menu of the file in the project pane hasmany useful comman

For Introduction to Java Programming, 5E By Y. Daniel Liang This supplement covers the following topics: Getting Started with JBuilder Creating a Project Creating, Compiling, and Running a Java Program Debugging in JBuilder Getting Help in JBuilder Creating and Testing Java Applets NOTE: To use this supplement with the text, cover Sections 1, 2, and 3 in this supplement .