Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help on exit button/panel

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help on exit button/panel

    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);
    	            }
    	        }
    Attached Images Attached Images


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help on exit button/panel

    You're trying to parse numbers even if exit is called causing the exception to be thrown before exiting can occur. A problem is that your Handler class is trying to do too much, trying to handle several buttons with completely distinct functions. I'd recommend a separate handler for each distinct button function such as an ExitHandler class for your exit button.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help on exit button/panel

    we already tried that by putting exit.addActuonListener(new ExitClass()); and we get error message saying cannot find symbol and sometimes illegal start of public class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help on exit button/panel

    exit.addActuonListener(new ExitClass()); and we get error message saying cannot find symbol
    Check your spelling for that class name: ExitClass. Where is it defined?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help on exit button/panel

    what do you mean by where is it defined?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help on exit button/panel

    The compiler can not find the definition for the class: ExitClass.
    Where is the ExitClass.class or the ExitClass.java file located?

    See the tutorial for how to declare/define a class:
    Declaring Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Trick - How to build a button to switch between panel colors
    By vividMario52 in forum Java Swing Tutorials
    Replies: 6
    Last Post: May 23rd, 2013, 06:55 AM
  2. [SOLVED] The button wouldn't show on the panel
    By nggdowt in forum AWT / Java Swing
    Replies: 7
    Last Post: November 3rd, 2011, 09:31 PM
  3. while loop wont exit
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2011, 11:42 AM
  4. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM