Exercise 1: Creating a Project
The first step is to create an IDE project for the application that we are going to develop. We will name our project NumberAddition .
Exercise 2: Building the Front End
To proceed with building our interface, we need to create a Java container within which we will place the other required GUI components. In this step we’ll create a container using the JFrame component. We will place the container in a new package, which will appear within the Source Packages node.
Create a JFrame container

Adding Components: Making the Front End
Next we will use the Palette to populate our application’s front end with a JPanel. Then we will add three JLabels, three JTextFields, and three JButtons. If you have not used the GUI Builder before, you might find information in the Designing a Swing GUI in NetBeans IDE tutorial on positioning components useful.
Renaming the Components
In this step we are going to rename the display text of the components that were just added to the JFrame.

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 to respond to events. 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.

Making the Clear Button Work
Making the Add Button Work
Exercise 4: Running the Program
To run the program in the IDE:

Choose Run > Run Project (Number Addition) (alternatively, press F6).

To run the program outside of the IDE:

Choose Run > Clean and Build Main Project (Shift-F11) to build the application JAR file.

Using your system’s file explorer or file manager, navigate to the ` NumberAddition/dist` directory.
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.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

package my.numberaddition;

//Written by:
// Date:
//Description: what the program does.
       // get 2 inputs from textbox,
       // show sum of 2 number if add button is clicked
       // show subtraction of 2 number if subtract button is clicked
       // clear all textboxes when clear button is clicked
       // close the program when exit button is clicked

public class NumberAdditionUI extends javax.swing.JFrame {

    * Creates new form NumberAdditionUI
    public NumberAdditionUI() {

    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

       jPanel1 = new javax.swing.JPanel();
       jLabel1 = new javax.swing.JLabel();
       jLabel2 = new javax.swing.JLabel();
       jLabel3 = new javax.swing.JLabel();
       jTextField1 = new javax.swing.JTextField();
       jTextField2 = new javax.swing.JTextField();
       jTextField3 = new javax.swing.JTextField();
       jButton1 = new javax.swing.JButton();
       jButton2 = new javax.swing.JButton();
       jButton4 = new javax.swing.JButton();
       jButton3 = new javax.swing.JButton();


       jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Number Addition"));

       jLabel1.setText("First Number:");

       jLabel2.setText("Second Number:");



By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats