站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题添加按钮的功能(初学者指南)(4)

添加按钮的功能(初学者指南)(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.

Exercise 4: Running the Program

The final step is to build and run the program.

  1. Click on Build in the Main Menu and Choose --> Build Main Project.

  2. When the Build output is finished, click on Run in the Main Menu and Choose --> Run Main Project

  3. If you get a window informing you that Project NumberAddition does not have a main class set, then you should select my.NumberAddition.NumberAdditionUI as the main class in the same window and click the OK button.

  4. Your created program is now running.

In this tutorial you learned how you hook up functionality to GUI components with the NetBeans GUI Builder Matisse.


How Event Handling Works

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  1. Go back to the file NumberAdditionUI.java in the Editor. Click the Source tab to see the GUI's source.

  2. Scroll down and note the methods jButton1ActionPerformed(), jButton2ActionPerformed(), and jButton3ActionPerformed() that you just implemented. These methods are called event handlers.

  3. 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.

  4. First, note the blue block around the initComponents() method. This code was auto-generated by the IDE and you cannot edit it.

  5. 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.

  6. 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.



↑返回目录
前一篇: 添加按钮的功能(初学者指南)(3)
后一篇: 在 Mac OS X上开发 GUI