当前页面: 开发资料首页 → Netbeans 专题 → 添加按钮的功能(初学者指南)(4)
摘要: This tutorial teaches you how to build a simple GUI with back-end functionality. This tutorial is geared to the beginner and introduces the basic construction of a GUI with functionality. A basic understanding of the Java Programming Language is required.
The final step is to build and run the program.
In this tutorial you learned how you hook up functionality to GUI components with the NetBeans GUI Builder Matisse.
This tutorial showed how to respond to s simple button event. There are many more events you can have your application respond to. The IDE can help you find the list of available events your GUI components can handle:
Go back to the file NumberAdditionUI.java
in the Editor.
Click the Design tab to see the GUI's layout in the GUI Builder Matisse.
Right-click any GUI component, and select Events from the pop-up menu. For now, just browse the menu to see what's there, you don't need to select anything.
Alternatively, you can select Properties from the Window menu. In the Properties window, click the Events tab. In the Events tab, you can view and edit events handlers associated with the currently active GUI component.
You can have your application respond to key presses, single, double and triple mouse clicks, mouse motion, window size and focus changes. You can generate event handlers for all of them from the Events menu. The most common event you will use is an Action event. (Learn best practices for Event handling from Sun's Java Events Tutorial.)
How does event handling work? Everytime you select an event from the Event menu, the IDE automatically creates a so-called event listener for you, and hooks it up to your component. Go through the following steps to see how event handling works.
Go back to the file NumberAdditionUI.java
in the Editor. Click the Source tab to see the GUI's source.
Scroll down and note the methods jButton1ActionPerformed()
,
jButton2ActionPerformed()
, and jButton3ActionPerformed()
that you just implemented.
These methods are called event handlers.
Now scroll to a method called initComponents()
. If you do not see this method,
look for a line that says Generated Code
; click the + sign next to it
to expand the collapsed initComponents()
method.
First, note the blue block around the initComponents()
method. This code was auto-generated
by the IDE and you cannot edit it.
Now, browse through the initComponents()
method. Among other things,
it contains the code that initializes and places your GUI components on the form.
This code is generated and updated automatically while you place and edit components in the Design view.
In initComponents()
, scroll down to where it says
jButton3.setText("Exit"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } });This is the spot where an event listener object is added to the GUI component; in this case, you register an ActionListener to the jButton3. The ActionListener interface has an actionPerformed method taking ActionEvent object which is implemented simply by calling your
jButton3ActionPerformed
event handler.
The button is now listening to action events. Everytime it is pressed an
ActionEvent is generated and passed to the listener's actionPerformed method
which in turn executes code that you provided in the event handler for this event.
Generally speaking, to be able to respond, each interactive GUI component needs to register to an event listener and needs to implement an event handler. As you can see, NetBeans IDE handles hooking up the event listener for you, so you can concentrate on implementing the actual business logic that should be triggered by the event.