0

I am to build a simple swing UI application to get input from user from separate JTextField and then finally click on a Button to perform addition to display on a JTextField below the JButton.

The input field(JTextField) is to receive integer values.

 package com.APPOne_G5;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class AppOne_G5 {
    private JPanel calculatorFrame;
    private JTextField inputField1;
    private JTextField inputField2;
    private JButton calculateButton;
    private JTextField outputField;
    private JLabel inputFieldLabel1;
    private JLabel inputFieldLabel2;
    private JLabel outputFieldLabel;

    private int number1, number2;
    private int calc = number1 + number2;

    public AppOne_G5() {
        inputField1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number1 = Integer.parseInt(inputField1.getText());

            }
        });
        inputField2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                number2 = Integer.parseInt(inputField2.getText());
            }
        });
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                 calc = number1 + number2;

                outputField.setText(String.valueOf(number1+number2));

            }
        });
        outputField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                outputField.setEditable(false);
                outputField.setText(String.valueOf(calc));

            }
        });
    }



    public static void main(String[] args) {
        JFrame app = new JFrame("Group 5 AppOne");
        app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
        app.setContentPane(new AppOne_G5().calculatorFrame);
        app.setLocationRelativeTo(null);
        app.setVisible(true);
        app.pack();

    }
}

I get Zero after i enter values to be added.

This is the GUI form of the application

  • I suggest that you only have one action listener that activates on the button click. However, to debug/check where your code is going wrong you can print information to the console to see if it is showing the values you expect `System.out.println("input1 listener value: "+ number1);` or `System.out.println(String.valueOf(number1+number2));` – sorifiend Mar 04 '21 at 02:03
  • Awesome thanks, but when I use System.out.println(....); It doesn't work because the constructor of the calculateButton Is of the type void so it doesn't return a value so when you use System.out.println(..); the compiler will you to make it of the type non void and that will also give and error – Joshua Nyarko Boateng Mar 04 '21 at 10:24
  • Yes, because the code you have in your question will not compile at all because it is missing a lot of information, you never create the buttons/labels and you never place them into the jPanel. You need to initialise them all, for example, `private JButton calculateButton;` needs to be `private JButton calculateButton = new JButton("Calculate");` etc, and you need to add it to the JPanel `calculatorFrame.add(calculateButton);`. I assume that you had this working originally otherwise you would not have been able to make this image https://i.stack.imgur.com/6uSIw.png – sorifiend Mar 04 '21 at 10:28
  • I have edited my answer with a fully working piece of code, just add your imports and give it a test. – sorifiend Mar 04 '21 at 10:41
  • Yes sure thanks very much my code is working now fine and i have read more about to use the JTextField......:) – Joshua Nyarko Boateng Mar 21 '21 at 13:29

2 Answers2

0

Further to my comment, you only need one action listener that activates when you click the button. Here is a complete working example of how it might work:

public class AppOne_G5 {
    private JFrame app = new JFrame("Group 5 AppOne");
    private JPanel calculatorFrame = new JPanel();
    private JTextField inputField1 = new JTextField("2");
    private JTextField inputField2 = new JTextField("5");
    private JButton calculateButton = new JButton("Calculate");
    private JTextField outputField = new JTextField("   ");
    private JLabel inputFieldLabel1 = new JLabel("Enter Integer");
    private JLabel inputFieldLabel2 = new JLabel("Enter Integer");;
    private JLabel outputFieldLabel = new JLabel("Result");

    private int number1, number2;
    private int calc;

    public AppOne_G5() {
        //Add components in order
        app.add(calculatorFrame);
        app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);
        calculatorFrame.add(inputFieldLabel1);
        calculatorFrame.add(inputField1);
        calculatorFrame.add(inputFieldLabel2);
        calculatorFrame.add(inputField2);
        calculatorFrame.add(calculateButton);
        calculatorFrame.add(outputFieldLabel);
        calculatorFrame.add(outputField);
        //Create action listener
        calculateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Get the inputs
                number1 = Integer.parseInt(inputField1.getText());
                number2 = Integer.parseInt(inputField2.getText());
                System.out.println("number 1: " + number1 + ", number2: " + number2);
                //Calculate the output
                calc = number1 + number2;
                System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);
                //Show the output
                outputField.setText(String.valueOf(calc));
            }
        });
        //Show JFrame
        app.setVisible(true);
        app.pack();
    }
    
    public static void main(String[] args) {
        new AppOne_G5();
    }
}

To debug why you are not getting the expected outupt you can use System.out.println("...");. For example if you were to use the following then you could see if the issue is beacause calc is wrong, or if numebr1 or number2 were already zero:

calc = number1 + number2;
System.out.println("number 1: " + number1 + ", number2: " + number2 + ", calc: "+calc);

Once you know where the issue is you can go further back in the code and work out why the value is zero.

sorifiend
  • 5,927
  • 1
  • 28
  • 45
0

As Scorfield mentions, all u need is just one ActionListener on the calculateButton. That's where u should get the text entered by the user.

Drex
  • 11
  • 1