Prob1.JPG
As You can See the code works perfectly and we were able to put a exit button/panel on it
here is the code
import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Calculator { JButton add,divide,multiply,sub,exit; JTextField num1,num2; JLabel ans; public Calculator(){ JFrame frame = new JFrame("Calculator - Rosopa"); num1 = new JTextField (10); ans = new JLabel(""); num2 = new JTextField (9); add = new JButton ("+"); divide = new JButton("/"); multiply = new JButton("X"); sub = new JButton ("-"); exit = new JButton ("EXIT"); frame.setVisible(true); frame.setBounds(500, 200, 290, 210); frame.setLayout(new GridLayout(0,2,1,1)); frame.add(new JLabel("First Number: ")); frame.add(num1); frame.add(new JLabel("Second Number: ")); frame.add(num2); frame.add(new JLabel("Answer: ")); frame.add(ans); frame.add(add); frame.add(sub); frame.add(multiply); frame.add(divide); frame.add(exit); add.addActionListener(new HandlerClass()); sub.addActionListener(new HandlerClass()); divide.addActionListener(new HandlerClass()); multiply.addActionListener(new HandlerClass()); exit.addActionListener(new HandlerClass()); } public static void main(String[] args) { new Calculator(); } public class HandlerClass implements ActionListener{ public void actionPerformed(ActionEvent ae){ try{ int fnum = Integer.parseInt(num1.getText()); int snum = Integer.parseInt(num2.getText()); if(ae.getSource()== add){ ans.setText(String.valueOf(fnum+snum)); } else if(ae.getSource()== sub){ ans.setText(String.valueOf(fnum-snum)); } else if(ae.getSource()== multiply){ ans.setText(String.valueOf(fnum*snum)); } else{ ans.setText(String.valueOf(fnum/snum)); } }catch(Exception e) { JOptionPane.showMessageDialog(null, "Please enter a valid number"); e.printStackTrace(); } } } }
Our Problem now is that whenever we click the "EXIT" button/panel to end the program we get a message saying "PLEASE ENTER A VALID NUMBER" As Shown Below
Prob2.JPG
We tried putting some codes for the "EXIT" button but we still get the same message ("PLEASE ENTER A VALID NUMBER")
import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Calculator { JButton add,divide,multiply,sub,exit; JTextField num1,num2; JLabel ans; public Calculator(){ JFrame frame = new JFrame("Calculator - Rosopa"); num1 = new JTextField (10); ans = new JLabel(""); num2 = new JTextField (9); add = new JButton ("+"); divide = new JButton("/"); multiply = new JButton("X"); sub = new JButton ("-"); exit = new JButton ("EXIT"); frame.setVisible(true); frame.setBounds(500, 200, 290, 210); frame.setLayout(new GridLayout(0,2,1,1)); frame.add(new JLabel("First Number: ")); frame.add(num1); frame.add(new JLabel("Second Number: ")); frame.add(num2); frame.add(new JLabel("Answer: ")); frame.add(ans); frame.add(add); frame.add(sub); frame.add(multiply); frame.add(divide); frame.add(exit); add.addActionListener(new HandlerClass()); sub.addActionListener(new HandlerClass()); divide.addActionListener(new HandlerClass()); multiply.addActionListener(new HandlerClass()); exit.addActionListener(new HandlerClass()); } public static void main(String[] args) { new Calculator(); } public class HandlerClass implements ActionListener{ public void actionPerformed(ActionEvent ae){ try{ int fnum = Integer.parseInt(num1.getText()); int snum = Integer.parseInt(num2.getText()); if(ae.getSource()== add){ ans.setText(String.valueOf(fnum+snum)); } else if(ae.getSource()== sub){ ans.setText(String.valueOf(fnum-snum)); } else if(ae.getSource()== multiply){ ans.setText(String.valueOf(fnum*snum)); } else{ ans.setText(String.valueOf(fnum/snum)); } }catch(Exception e) { JOptionPane.showMessageDialog(null, "Please enter a valid number"); e.printStackTrace(); } } } } class ExitButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { System.exit(0); } }