CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    another calculator problem

    I don't know why this code doesn't work as it should?
    I am a newbie in programming so any extra explanation would be very helpful.

    Code:
    public class Kalkulator {
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run()
    			{
    				JPanel plosca = new Plosca();
    				GraficnoOgrodje.ustvariOkno("Kalkulator",plosca,false);
    			}
    		});
    
    	}
    	private static class Plosca extends JPanel implements ActionListener
    	{
    		private JTextField vpSt1, vpSt2, vpRez;
    		private JButton gmPlus,gmMinus,gmDeljeno,gmKrat;
    	
    		public Plosca()
    		{
    			setLayout(new FlowLayout());
    			
    			vpSt1 = new JTextField(7);
    			vpSt2 = new JTextField(7);
    			vpRez = new JTextField(7);
    			JButton gmPlus = new JButton(" + ");
    			JButton gmMinus = new JButton(" - ");
    			JButton gmKrat = new JButton(" * ");
    			JButton gmDeljeno = new JButton(" / ");
    			
    			add(new JLabel("Stevilka 1"));
    			add(vpSt1);
    			add(new JLabel("Stevilka 2"));
    			add(vpSt2);
    			add(new JLabel("Rezultat"));
    			add(vpRez);
    			add(gmPlus);
    			add(gmMinus);
    			add(gmKrat);
    			add(gmDeljeno);
    			
    			vpSt1.setHorizontalAlignment(JTextField.RIGHT);
    			vpSt2.setHorizontalAlignment(JTextField.RIGHT);
    			vpRez.setHorizontalAlignment(JTextField.RIGHT);
    			vpRez.setEditable(false);
    			
    			gmPlus.addActionListener(this);
    			gmMinus.addActionListener(this);
    			gmKrat.addActionListener(this);
    			gmDeljeno.addActionListener(this);
    		}
    		
    			public void actionPerformed(ActionEvent e)
    			{
    				
    				double stev1 = Double.parseDouble(vpSt1.getText());
    				double stev2 = Double.parseDouble(vpSt2.getText());
    				double rez = 7; 
    				
    				Object izvor = e.getSource();
    				if( izvor == gmPlus )
    				{
    					rez = stev1 + stev2;
    					System.out.println("Hi");
    				}
    				else if(izvor == gmMinus)
    				{
    					rez = stev1 - stev2;
    				}
    				else if(izvor == gmKrat)
    				{
    					rez = stev1 * stev2;
    				}
    				else if(izvor == gmDeljeno)
    				{
    					rez = stev1 / stev2;
    				}
    				vpRez.setText(Double.toString(rez)); 	
    				
    			}
    	}
    	
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    I don't know why this code doesn't work as it should?
    Given that you haven't said what it is doing and what it should do, I don't know either

    It also doesn't help that your variable names are in a language I don't understand, so please can you provide some English comments in the code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    This is a calculator, I didnt make any notes becase i thought it is an easy program.

    Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.Event.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class Kalkulator {          //Calculator
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run()
    			{
    				JPanel plosca = new Plosca();
    				GraficnoOgrodje.ustvariOkno("Kalkulator",plosca,false); //Creates a window
    			}
    		});
    
    	}
    	private static class Plosca extends JPanel implements ActionListener
    	{
    		private JTextField vpSt1, vpSt2, vpRez;  // Textfields for inputting numbers Rez = result
    		private JButton gmPlus,gmMinus,gmDeljeno,gmKrat; // Buttons- minus, plus ...
    	
    		public Plosca()
    		{
    			setLayout(new FlowLayout());
    			
    			vpSt1 = new JTextField(7);
    			vpSt2 = new JTextField(7);
    			vpRez = new JTextField(7);
    			JButton gmPlus = new JButton(" + ");
    			JButton gmMinus = new JButton(" - ");
    			JButton gmKrat = new JButton(" * ");
    			JButton gmDeljeno = new JButton(" / ");
    			
    			add(new JLabel("Stevilka 1"));  // Stevilka = number
    			add(vpSt1);
    			add(new JLabel("Stevilka 2"));
    			add(vpSt2);
    			add(new JLabel("Rezultat")); // just a label for a result
    			add(vpRez);
    			add(gmPlus);
    			add(gmMinus);
    			add(gmKrat);
    			add(gmDeljeno);
    			
    			vpSt1.setHorizontalAlignment(JTextField.RIGHT);
    			vpSt2.setHorizontalAlignment(JTextField.RIGHT);
    			vpRez.setHorizontalAlignment(JTextField.RIGHT);
    			vpRez.setEditable(false);
    			
    			gmPlus.addActionListener(this);
    			gmMinus.addActionListener(this);
    			gmKrat.addActionListener(this);
    			gmDeljeno.addActionListener(this);
    		}
    		
    			public void actionPerformed(ActionEvent e)
    			{
    				
    				double stev1 = Double.parseDouble(vpSt1.getText());
    				double stev2 = Double.parseDouble(vpSt2.getText());
    				double rez = 7; 
    				
    				Object izvor = e.getSource(); // izvor = source
    				if( izvor == gmPlus )
    				{
    					rez = stev1 + stev2;
    				
    				}
    				else if(izvor == gmMinus)
    				{
    					rez = stev1 - stev2;
    				}
    				else if(izvor == gmKrat)
    				{
    					rez = stev1 * stev2;
    				}
    				else if(izvor == gmDeljeno)
    				{
    					rez = stev1 / stev2;
    				}
    				vpRez.setText(Double.toString(rez)); 	
    				
    			}
    	}
    	
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    You still haven't said what bit doesn't work.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    I dont know why are all conditions in all IF statesmant always negative(false)?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    You are assigning the JButtons to local variables with exactly the same name as your instance variables so your instance variables will all be null and hence will never match the event source.

    Note: it's nearly always a bad idea to have variables with the same name but different scopes. It leads to all sorts of confusion as you have just found out.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    Can you explain this a little bit more. What should i fix in this code in order for this program to work?

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    Your code declares an instance variable gmPlus of type JButton but doesn't ever assign anything to it.

    Look at the line where you assign the JButton instance to your variable gmPlus. You will see you are not assigning it to the instance variable gmPlus but to a local variable called gmPlus.

    To solve the problem remove the declaration of type JButton from the beginning of the line. ie

    Change
    Code:
    JButton gmPlus = new JButton(" + ");
    to
    Code:
    gmPlus = new JButton(" + ");
    This applies to all your operator buttons.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    oh, i understand. I created 2 variables with the same name(JButton gmPlus) - inside the function and
    - one inside of class

    function Action listener was called from the different object.

  10. #10
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    How is it possible that we never call actionPerformed and Run functions? We just declare them.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    How is it possible that we never call actionPerformed and Run functions? We just declare them.
    These methods are called by the classes you have registered your object with. For instance looking at ActionPerformed: You declare a class as implementing the ActionListener interface which means you have to implement the ActionPerformed method. You then pass an instance of this class to any/all GUI components that you want to listen to for action events ie by calling their addActionListener(..) method. When the GUI component generates an action event it iterates over it's list of registered listeners and calls each one's actionPerformed(..) method and hence your object's actionPerformed(..) method is called.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: another calculator problem

    so actually the JVM calls these functions?

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: another calculator problem

    Only in the sense that everything that happens in Java goes through the JVM.

    The sequence of events is roughly:

    • You click on button on the screen.
    • The OS sends a OS mouse click event
    • This OS event is translated to a Java mouse click event and is added to the EventQueue.
    • The EventQueue dispatches the mouse click event to all listeners. This is done by the EventDispathThread which is the single thread that runs all swing stuff.
    • Your button gets sent the event and generates an ActionEvent which it sends to all its ActionListeners.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured