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

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

添加按钮的功能(初学者指南)(3)

摘要: 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.

Renaming the Components

In this step we are going to rename the components that were just added to the JFrame.

  1. Double-click on jLabel1 and change the text property to First Number
  2. Double-click on jLabel2 and change the text to Second Number
  3. Double-click on jLabel3 and change the text to Result
  4. Double-click on jTextField1 and remove the sample text in the text field. You may have to resize the jTextField1 to its original size. Repeat this step for jTextField2 and jTextField3.
  5. Double-click on jButton1 and rename it Clear

  6. Double-click on jButton2 and rename it Add

  7. Double-click on jButton3 and rename it Exit

Your Finished GUI should now look like this:



Exercise 3: Adding Functionality

In this exercise we are going to give functionality to the Add, Clear, and Exit buttons. The jTextField1 and jTextField2 boxes will be used for user input and jTextField3 for program output - what we are creating is a very simple calculator. Let's begin.

Making the Exit Button Work

In order to give function to the buttons, we have to assign an event handler to each, responding to an event. In our case we want to know when the button is pressed, either by mouse click or via keyboard. So we will use ActionListener responding to ActionEvent.

  1. Right Click on the Exit button. From the pop-up menu choose Events --> Action --> ActionPerformed. Note that the menu contains many more events you can respond to! When you select the actionPerformed event, the IDE will automatically add an ActionListener to the Exit button and generate a handler method for handling the listener's actionPerformed method.

  2. The IDE will open up the Source Code window and scroll to where you implement the action you want the button to do when the button is pressed (either by mouse click or via keyboard). Your Source Code window should contain the following lines:
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        //TODO: Add your handling code here:
    }
  3. We are now going to add code for what we want the Exit Button to do. You will have to type System.exit(0); to the above code, replacing the TODO line. Your finished Exit button code should look like this:
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    } 

Making the Clear Button Work

  1. Click on the Design tab at the top of your work area to go back to the Form Design
  2. Right Click on the Clear button (jButton1). From the pop-up menu select Events --> Action --> actionPerformed.

  3. We are going to have the Clear button erase all text from the jTextFields. To do this, you will add some code like above. Your finished source code should look like this:

  4. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
    }
The above code changes the text in all three of our jTextFields to nothing, in essence it is overwriting the existing Text with a blank.

Making the Add Button Work

The Add button will perform three actions.

  1. It is going to accept user input from jTextField1 and jTextField2 and convert the input from a type String to a float.
  2. It will then perform addition of the two numbers and finally,
  3. it will convert the sum to a type String and place it in jTextField3.
Lets get started!
  1. Click on the Design tab at the top of your work area to go back to the Form Design.

  2. Right- click on the Add button (jButton3). From the pop-up menu, select Events --> Action --> actionPerformed
  3. We are going to add some code to have our Add button work. The finished source code shall look like this:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
        // First we define float variables.
        float num1, num2, result;
        // We have to parse the text to a type float.
        num1 = Float.parseFloat(jTextField1.getText());
        num2 = Float.parseFloat(jTextField2.getText());
        // Now we can perform the addition.
        result = num1+num2;
        // We will now pass the value of result to jTextField3.
        // At the same time, we are going to
        // change the value of result from a float to a string.
        jTextField3.setText(String.valueOf(result));
    }

Our Program is now complete we can now build and run it to see it in action.



↑返回目录
前一篇: 添加按钮的功能(初学者指南)(2)
后一篇: 添加按钮的功能(初学者指南)(4)