|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Creating Your First ApplicationIts time to create an application using Visual J++. Your application will be in the form of a compiled .EXE. Whereas the applet we created was loaded into Internet Explorer and then executed, the compiled application doesnt require Internet Explorer. Compiled Visual J++ applications are normally very small. This is because they dont contain the runtime code thats necessary for the application to run. So how does the application actually run when you execute the .EXE file? The compiled Visual J++ application almost immediately executes a program named WEXESTUB.EXE. This is part of the redistributable Visual J++ files. This program in turn invokes the Java Virtual Machine in such a way that it executes the byte code of the compiled application.
You might ask yourself why Visual J++ would go through all that trouble. It might seem as though spawning an extra .EXE and then invoking the Java Virtual Machine is a lot of trouble. And you run the risk of encountering problems with both. What if WEXESTUB.EXE is missing or damaged, or what if the Java Virtual Machine is somehow missing? All of these are good questions. But the fact is, Java has always relied on another application to correctly execute. Browsers such as Netscape and Internet Explorer are how users have executed Java for years. And the Java Virtual Machine has always been a required element of that. So its not too unusual that a similar system is part of executing Visual J++ applications. These small applications take up less spaceprobably about a tenth the size of what a self-contained application would be. That size reduction is not trivial when youre talking about installing to someones hard drive. Installation files will be smaller, and this might be very important if the installation files are downloaded from the Web. Another good reason to rely on the Java Virtual Machine is that as its updated, the applications run better and faster. The applications dont change, just the Java Virtual Machine does. So application developers benefit when users upgrade to a newer version of the Java Virtual Machine without having to deploy another version of their application.
Now to create the application. If youve just started Visual J++, the New Projects dialog box will open. If Visual J++ is already running, select File, New Project and the New Projects dialog will appear. This time, well select Applications in the Visual J++ Projects folder. As you can see in Figure 1.6, there are three application choices: Windows Application, Console Application, and Application Wizard. Well choose Windows Application.
Name your project Hello2. As you can see in Figure 1.6, the directory in which Im creating the project is VJProjects\Hello2. Click the OK button and the project will be created. After the project is created and you double-click on Form1.java in the Project Explorer window, youll see the default form in the center of the screen, as shown in Figure 1.7. This is a canvas on which you can add items from the toolbox by dragging and dropping.
Lets start off by adding a label to the form. In the toolbox, select Label. Move the mouse inside of the form, and youll notice that the mouse cursor changes shape, indicating that youre ready to put a label on the form. Point the mouse to the location at which youd like the upper-left corner of the label to appear, and hold down the left mouse button. While holding down the left mouse button, draw the mouse down and to the right until the label is the desired size. You can always change the labels size and position with the mouse. After the label is placed on the form as shown in Figure 1.8, youll notice that the Properties window changes to the properties of a label. The list contains quite a few properties, all of which change the appearance of the label. The one were going to change now is the text property. Click in the text property field and change the text to Hello Visual J++ World. As you type, the label in the form will change.
Compile the application by selecting Build, Build. Then select Debug, Run. Your application will appear, displaying your Hello Visual J++ World message. By now youre probably wondering where the source code is for the application. Visual J++ makes every effort to keep things visual so that developing applications is easy and intuitive. Thats why you see the form and not the source code by default. But to see the source code in the editor is easy. Just double-click on the form (not the form thats executing as an application, but the form thats part of the Visual J++ editor). Scroll down through the source code. Youll see the following lines as a result of adding the label from the toolbox: label1.setLocation(new Point(20, 30)); label1.setSize(new Point(210, 40)); label1.setText("Hello Visual J++ World"); label1.setTabIndex(1); this.setNewControls(new Control[] { label1}); Lets experiment. Go back to the form by double-clicking on Form1.java in the Project Explorer window. Then, click on your label so that its properties are shown in the Properties window. In the font property field, click on the button with the label, and a font selector dialog will appear. Now, change the font size to 14 and click the OK button. Finally, edit the location property to a value of 10, 50. Build the program and then go to the source code. The changes will be reflected in the source code, even though you didnt touch the source code. Your source code should look as shown next.
label1.setFont(new Font("MS Sans Serif", -19)); label1.setLocation(new Point(10, 50)); label1.setSize(new Point(210, 40)); label1.setText("Hello Visual J++ World"); label1.setTabIndex(1); this.setNewControls(new Control[] { label1}); Your applications label displays text thats larger than before. Now lets add another label below the existing one with text that says Visual J++ is Cool. When you rebuild your application, it will look something like the one in Figure 1.9.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |