1

Context

I'm trying to build a percentage calculator using JFrame and event handling by having blank text fields and "enter" buttons and displaying the answer depending on which button they click (two different calculations for each button).

Problem

Thing is, when I put the JOptionPane.showMessageDialog line inside of the if/else if statement (all in the actionPerformed method), the program runs, but clicking the buttons does nothing. But if I put it outside of the statements, it says the answer variable is undeclared.

I realize a percentage calculator can be done in a million other, simpler ways, but this is simply what I had in mind and wanted to see if I could do it (I'm a complete beginner).

Code

public class PercentageCalc extends JFrame{
private JTextField item2, item4, item5, item7;
private JButton button, button2;

public PercentageCalc(){
    super("Percentage Calculator");
    setLayout(new FlowLayout());

    JLabel item1 = new JLabel("What is");
    add(item1);

    JTextField item2 = new JTextField(3);
    add(item2);

    JLabel item3 = new JLabel("% of");
    add(item3);

    JTextField item4 = new JTextField(2);
    add(item4);
    button = new JButton("Enter");
    add(button);
    JLabel spacer = new JLabel("                       ");
    add(spacer);

    JTextField item5 = new JTextField(3);
    add(item5);

    JLabel item6 = new JLabel("is what % of");
    add(item6);

    JTextField item7 = new JTextField(3);
    add(item7);
    button2 = new JButton("Enter");
    add(button2);


    thehandler handler = new thehandler();
    button.addActionListener(handler);
    button2.addActionListener(handler); 
}
private class thehandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        String strx;
        String stry;
        double x, y, answer;
        if(e.getSource()==button){
            strx = item2.getText();
            stry = item4.getText();
            x = Double.parseDouble(strx);
            y = Double.parseDouble(stry);
            answer = y * 0.01 * x;
            JOptionPane.showMessageDialog(null, answer);
        }
            else if(e.getSource()==button2){
            strx = item5.getText();
            stry = item7.getText();
            x = Double.parseDouble(strx);
            y = Double.parseDouble(stry);
            answer = x/y*100;
            JOptionPane.showMessageDialog(null, answer);
            }
}
Omar Eldeeb
  • 13
  • 1
  • 4

2 Answers2

1

You have member-variable and local variable with the same name. Please read ere and here about it.

Here is the corrected variant of you program

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class PrecentageCalc extends JFrame {
    private JTextField item2, item4, item5, item7;

    private JButton button, button2;

    public PrecentageCalc() {
        super("Percentage Calculator");
        setLayout(new FlowLayout());

        JLabel item1 = new JLabel("What is");
        add(item1);

        item2 = new JTextField(3);
        add(item2);

        JLabel item3 = new JLabel("% of");
        add(item3);

        item4 = new JTextField(2);
        add(item4);
        button = new JButton("Enter");
        add(button);
        JLabel spacer = new JLabel("                       ");
        add(spacer);

        item5 = new JTextField(3);
        add(item5);

        JLabel item6 = new JLabel("is what % of");
        add(item6);

        item7 = new JTextField(3);
        add(item7);
        button2 = new JButton("Enter");
        add(button2);

        thehandler handler = new thehandler();
        button.addActionListener(handler);
        button2.addActionListener(handler);
    }

    private class thehandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String strx;
            String stry;
            double x, y, answer;
            if (e.getSource() == button) {
                strx = item2.getText();
                stry = item4.getText();
                x = Double.parseDouble(strx);
                y = Double.parseDouble(stry);
                answer = y * 0.01 * x;
                JOptionPane.showMessageDialog(null, answer);
            } else if (e.getSource() == button2) {
                strx = item5.getText();
                stry = item7.getText();
                x = Double.parseDouble(strx);
                y = Double.parseDouble(stry);
                answer = x / y * 100;
                JOptionPane.showMessageDialog(null, answer);
            }
        }
    }

    public static void main(String[] args) {
        JFrame frm = new PrecentageCalc();
        frm.pack();
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setVisible(true);
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • Making these changes causes the error: `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException` to appear when I click the button (although the Program still compiles and runs). – Omar Eldeeb Sep 05 '17 at 15:37
  • I just edited the original post and posted the entire code (minus the imported stuff and the main method, which isn't really relevant). – Omar Eldeeb Sep 05 '17 at 15:43
  • Thank you! I appreciate the help :) Also, thanks for the links. – Omar Eldeeb Sep 06 '17 at 19:43
0

Use separate ActionListeners for your buttons. Both use the same calculation method with different parameters:

public PercentageCalc() { 

  // Part of your code   

  button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
      handleInput(item2.getText(), item4.getText());
    }
  });

  button2.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
      handleInput(item5.getText(), item7.getText());
    }
  });

}

private void handleInput(final String strx, final String stry) {
  final double x = Double.parseDouble(strx);
  final double y = Double.parseDouble(stry);
  final double answer = y * 0.01 * x;
  JOptionPane.showMessageDialog(null, answer);
}
Maia
  • 71
  • 1
  • 9