By Dana Nourie, May 22, 2005, Updated July 2008 |
Download NetBeans IDE |
Goto
declaration and Goto
classFigure 1.Welcome Screen |
build.xml
), which contains instructions that the IDE uses when you perform commands on your project, such as compile
or run
Figure 3.Files Window |
nbproject/build-impl.xml
-- Contains the instructions for handling your project. The IDE generates this file, which you should not edit.build.xml
-- Imports targets from build-impl.xml
. You can edit this file to override existing targets or create new targets.Tip: Many menu commands such as Run and Build have keyboard shortcuts to the main project. You can also access all commands for individual projects by right-clicking their project nodes in the Projects window. |
build
folder after you compile the application.Main.java
source file to see the Source Editor similar to the one in Figure 4.Figure 5.Add Property Dialog |
getter
method and a setter
method for a field and optionally updates all referencing code to access the field using the getter
and setter
methods.JUnit
tests. The IDE also lets you debug applications that are running on a remote machine by attaching the debugger to the application process.You can use two project templates to import your existing source code:
The following steps cover use of the first template to create a standard Java project without the use of existing Ant scripts.
Your project will now be displayed in the Projects and Files windows. |
|
JFrame
, JDialog
, and JApplet
. Every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root. Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and added to the second. Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI. You have the option to add a menu bar to a top-level container.SamplesJFrame
, and click Finish.SamplesJFrame
to view the file in the Source Editor on the right.Figure 8.GUI Design Editor tool bar |
JFrame
container. Now you can easily add other components to it by selecting a component from the Palette and placing it in the Design Editor. Changing component properties is just as easy.Figure 10.NetBeans IDE Work Environment |
JFrame
class called SamplesJFrame
. Next, you'll build on the JFrame
component and add other components.JTabbedPane
. Notice that all the components you've created are listed in the Inspector window.JTabbedPane
in the editor (you may need to resize the JTabbedPane
so it is larger. A JPanel
is created. Note that a new tabbed pane has been added to JTabbedPane
.JPanel
to the JTabbedPane
in the Design Editor. Another tab appears. You can add as many tabs as you like, but this tutorial covers only two.Figure 12.Design Editor displaying a Tabbed Pane |
JPanel
in the Inspector window. For instance, click on jPane11
to select it. Next, go to the Properties window on the right and scroll down until you see the Tab Title
listed. Click on the ellipsis button ( ...
) at the right, and a window pops up that allows you to replace the current text. Type in Pay Calculator
and click OK. Notice that the text instantly appears in the Design view on the tab. Now change the text for jPanel2
; select it in the Inspector window, then in the Properties window enter Images
in the Tab Title
field.JPanel
you added. You can easily change the layout manager for components from the Inspector. For example, right-click on jPanel1
, and note the options listed from Set Layout. The IDE's GUI builder, named Matisse, uses the Free Design layout manager by default. This layout manager uses simple layout rules that are easy to work with.What's a Layout Manager? A layout manager is an object that implements the LayoutManager interface and determines the size and position of the components within a container. Although components can provide size and alignment hints, a container's layout manager has the final say on the size and position of the components within the container. See A Visual Guide to Layout Managers. |
JPanel
objects. If necessary, resize the jTabbedPanel
so that it fills the entire JFrame
that contains it.jPanel
, select jPanel1
in the Inspector or click on the Pay Calculator
tab in the Design Editor, then do the following:jPanel1
area in the Design Editor. This adds a JLabel
to the form.jPanel1
area in the Design Editor. Do this again, so there are two JTextField
s in total.JButton
to the form by selecting Button in the Palette, then clicking inside the jPanel1
area in the Design Editor.Variable | Text |
jLabel1 | Hours Worked: |
jLabel2 | Rate/ Hour ($): |
jLabel3 | Regular Pay: |
jLabel4 | Overtime Pay: |
jLabel5 | Total: |
jButton1 | Compute |
Variable | Text |
jLabel6 | ********** |
jLabel7 | ********** |
jLabel8 | ********** |
jTextField1 | [blank] |
jTextField2 | [blank] |
Figure 14.Design Editor Displaying Renamed Components |
JTextField
object and select Change Variable Name. Type in the name hoursWorked
. Then rename the second JTextField
object rate
.JFrame
listed and choose Add From Palette > Swing Menus > Menu Bar. This adds a JMenuBar
.JMenuBar
that has been added to the Inspector window and notice that two JMenu
objects have been added by default. In the Design Editor, note that the text of the two JMenu
items is File
and Edit
, respectively. While you can use the Properties window to change the values of all properties of a given object, you can often change text directly in the Design Editor. Change the text of the second menu item from Edit
to Help
. You can do so by double-clicking directly on the text in the editor.jMenu1
and choose Add From Palette > Menu Item. This creates a JMenuItem
.OpenItem
. You can do so by right-clicking the component either in the Design Editor or the Inspector and choosing Change Variable Name. If for any reason you have difficulty viewing the menu item in the Design Editor, you can double-click on jMenuBar1
in the Inspector, then click on the first jMenu
component (i.e., with File
text).OpenItem
to Open
. Right-click on the item in the Inspector and choose Edit Text. Otherwise, simply double-click on the default text in the Design Editor and type in Open
.ExitItem
, and change the default text to Exit
.JFrame
, then click on jMenuBar1
. Your menu bar should now look like the one in Figure 15.Icon
interface. Swing provides a particularly useful implementation of the Icon interface: ImageIcon
, which paints an icon from a GIF, JPEG, or (as of version 1.3) PNG image.JLabel
, and change the icon
property of that object to display the image.JPanel
object for the Images tab in the Inspector (or click on the Images tab in the Design Editor).JLabel
to the Images panel by clicking on Label in the Palette, then clicking inside the Images JPanel
area in the Design Editor....
) for the icon
property. A dialog displays, allowing you to locate and set the image to the property.Image chooser
is selected in the top drop-down box, then select the External Image option. Click the ellipsis button next to the File or URL field and navigate to the image you saved on your computer. You should see an image appear in the preview pane at the bottom of the dialog (Figure 16). Click OK.JLabel
's default text that now appears next to the image. Delete it.Figure 16.Inspector window |
JLabel
and accompanying image, click into the Source view for SamplesJFrame.java
, and you will see the following lines:JLabel
, and the second creates a new ImageIcon
object and attaches the image to it. It then sets the ImageIcon
to the JLabel
.ActionListener
interface. When implementing this interface, you must also implement a listener method.listener
method.String
. You'll need to parse these strings into workable types, such as int
or double
. You'll then need to perform the calculations to determine how many hours are regular and how many are overtime. Once those amounts are determined, you can calculate regular pay and overtime pay, then total those two amounts. Lastly, you'll need to convert the numbers to dollar amounts.Integer
, Double
, and NumberFormat
.int
, float
, or double
). The problem is that when you read from a file, get a command line argument, get an applet parameter, or just prompt for input from a text field on a form, you always start with a String
object. Treating that String
as a primitive requires an extra step, the conversion.String
you use a wrapper class. Every primitive has an associated wrapper class. These wrapper classes allow you to treat primitives as objects. In addition, these classes contain methods that permit manipulation of these objects, appropriate for the data type.Character
class, has a method that allows you to convert from a String
to the specific primitive type. Simply call the method from the appropriate wrapper class, and your String
is converted to a primitive type:String
variable myString
to an int
named myInt
. The conversion is that easy. The only trick is that the conversion for each data type involves a uniquely named method in each wrapper class. All but the Boolean conversions are done by similarly named methods, but still all the method names are different:Character
class has no such method, so you have to ask String
for the character with the charAt()
method:String
contents cannot be converted to the requested primitive type, then a NumberFormatException
is thrown. This is a runtime exception, so the conversion code does not have to be in a try-catch block.NumberFormat
class is used to print the numbers correctly. Printing numbers to the screen can produce some odd or undesired results. For instance:NumberFormat
abstract class in the java.text package
. This class provides the interface to format and parse numbers, and it includes methods to determine which locales have number formats and what their names are.getInstance
or getNumberInstance
gets a format for the normal number format, such as 600,000.getCurrencyInstance
gets a format for the currency number format, such as $600,000.00.getPercentInstance
gets a format for displaying percentages, such as 56%.NumberFormat
by calling on one of the above methods.jButton1ActionPerformed()
event handler method. Read the comments in the code, which provide some explanation. If you are new to using if/else
statements, read Java Programming Language Basics: if/else and switch statements, which is a part of the Java Technology Fundamentals newsletter.NumberFormat
and Locale
. You can do this by right-clicking in the Source Editor and choosing Fix Imports. A dialog displays (Figure 19), showing you the fully qualified class names that match the unrecognized classes used in the file.ExitItem
and choose Events > Action > actionPerformed. You'll be sent to the Source Editor, where you can see the following code:OpenItem
. You'll see that cleanup is an easy process.dist
> javadoc
folder, and you'll see output provided in the Output window. Then a browser window pops up with the documentation formatted in HTML. Click on SamplesJFrame
, and scroll down to see the documentation. This application would not require much documentation, but if you continue building in this project or move on to other larger projects, Javadoc will come in handy.