Jump to content

My Calc in Java GUI

- - - - -

  • Please log in to reply
5 replies to this topic

#1
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Posted Image




import javax.swing.*;

import java.awt.*;

import java.awt.event.*;




public class gui 

{

	

		  public static void main(String[] args) 

		  {

			  

			 new calc();

		

		        

	      }


}



class calc extends JFrame{

	

	private  JPanel leftPanel;

	private  JPanel CenterPanel;

	private  JPanel RightPanel;

	

	

	private  JTextField input1;

	private  JTextField input2;

	private  JLabel answer;

	

	private JButton plius;

	private JButton minus;

	private JButton gamr;

	private JButton gako;

	 

	

public calc(){	 // constructor

	

	  JFrame Frame = new JFrame("Vakhtang Koroghlishvili");

	  Frame.setSize(400, 200);

	  Frame.setLocation(300,300); 

      Frame.setVisible(true);

	  

	  Container contentPane =Frame.getContentPane();

	  contentPane.setLayout(new BorderLayout());

	  

	  contentPane.add(new JLabel("Calculator In Java", JLabel.CENTER), BorderLayout.NORTH);

	  

	  JPanel leftPanel = new JPanel();

	  leftPanel.setLayout(new GridLayout(3,1));

	  leftPanel.add(new JLabel(" A ="));

	  leftPanel.add(new JLabel(" B ="));

	  leftPanel.add(new JLabel(" "));

	  contentPane.add(leftPanel, BorderLayout.WEST);

	  

	  

	  

	  JPanel CenterPanel= new JPanel();

	  CenterPanel.setLayout(new GridLayout(3,1));  

	[COLOR="#B22222"][B]  final JTextField input1=new JTextField();

	  final  JTextField input2=new JTextField();

	  final JLabel answer = new JLabel();[/B][/COLOR]

	  CenterPanel.add(input1);

	  CenterPanel.add(input2);

	  CenterPanel.add(answer);  

	  contentPane.add(CenterPanel, BorderLayout.CENTER);

	  

	  

	  

	 JPanel RightPanel=new JPanel();

	 RightPanel.setLayout(new GridLayout(4,1));

	 JButton plius = new JButton("+");

	 JButton minus = new JButton("-");

	 JButton gamr = new JButton("*");

	 JButton gako = new JButton("/"); 	 

	 RightPanel.add(plius);

	 RightPanel.add(minus);

	 RightPanel.add(gamr);

	 RightPanel.add(gako); 

	 contentPane.add(RightPanel, BorderLayout.EAST);

	

	 ActionListener l = new ActionListener(){

			public void actionPerformed(ActionEvent e) {					

				double d1=Double.parseDouble(input1.getText());

				double d2=Double.parseDouble(input1.getText());	

				double d3=d1+d2;

				answer.setText("A+b="+d3);				

			}			

		  };

	 plius.addActionListener(l);

	 

	 

	 ActionListener l2 = new ActionListener(){

			public void actionPerformed(ActionEvent e) {					

				double d1=Double.parseDouble(input1.getText());

				double d2=Double.parseDouble(input1.getText());	

				double d3=d1-d2;

				answer.setText("A-b="+d3);				

			}			

		  };

	 minus.addActionListener(l2);

	 

	 



	 

	}


	 

}

	



I have some question:
1) I'm writing programs in Eclipse Java. Eclipse tells me that:
JTextField input1, and JLabel answer, must be final. I don't understand, why?
2) Sometimes the result is not correct, for example when I write 5+6, the program tells me that it is 10.0.
I don't understand why?

Attached Files


GNU/Linux Is the Best.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
First the easy thing, you calculate 5+6 = 10, cause you actually do 5+5=10 Because double d2=Double.parseDouble(input1.getText()); should use input2 instead.

Then, why must they be final.

The actionlisteners you've declared there are known as anonymous inner classes.
The reason that all variables 'from outside' must be declared final is because when the method ends, all the local variables would "die"-stop exist-get removed-get garbage collected.

But that instance of the anonymous inner class doesn't have to die with it, like in this example, you attached the instance to a button, so it's still alive. Because it's possible that instances of that inner class still exist it will still need access to those variables.
So what the inner class needs is a copy of the variables, so it can keep using them. To prevent inconsistency, you're obliged to declare those variables final to make sure that the variable you're using in the inner class is the same as the one outside at all times.

Note that final only means the reference can't change, you can still change anything in the object itself. Example:
public class X {
  public int x; //don't use public for class members, it's bad encapsulation.
}

class Y{
  public static void main(String[] args){
     final X variable = new X();
     variable.x = 2; //still works
  }
}


#3
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

wim DC said:


So what the inner class needs is a copy of the variables, so it can keep using them. To prevent inconsistency, you're obliged to declare those variables final to make sure that the variable you're using in the inner class is the same as the one outside at all times.

"copy of the variables" - In the other words, I have the other variable in the inner class that is referenced to the variable outside the innerclass?

wim DC said:

The reason that all variables 'from outside' must be declared final is because when the method ends, all the local variables would "die"-stop exist-get removed-get garbage collected.

everything is clear, but when the method ends I don't need the value of the variable. I need this only in the method, for example:

 ActionListener l = new ActionListener(){

			public void actionPerformed(ActionEvent e) {					

				double d1=Double.parseDouble(input1.getText());

				double d2=Double.parseDouble(input1.getText());	

				double d3=d1+d2;

				[COLOR="#B22222"]answer.setText("A+b="+d3);	[/COLOR]	 

                                 //  then, next I don't need the variable		

			}			

		  };


wim DC said:

Note that final only means the reference can't change

everything is clear. thanks. understood :)

if I have:
public static void main(String[] args){

	 final int a=10;   

	 a=11; 

	}
a is a reference variable. when I write a=11, I'm tring to change the reference. So , because of "a" is a final variable I cant chage it. :)
GNU/Linux Is the Best.

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

Quote

The reason that all variables 'from outside' must be declared final is because when the method ends, all the local variables would "die"-stop exist-get removed-get garbage collected.
everything is clear, but when the method ends I don't need the value of the variable. I need this only in the method, for example:
...
Perhaps I was a bit unclear, but in my quote I meant, when the constructor ends, all variables declared there will die, but the code from the actionListener can still run after the constructor has ended (when you click the button), therefor, the actionlistener still needs the variables from inside the constructor when it ran, thus they should be declared final so it's safe for the actionlistener to copy the references.

#5
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

wim DC said:

Perhaps I was a bit unclear, but in my quote I meant, when the constructor ends, all variables declared there will die, but the code from the actionListener can still run after the constructor has ended (when you click the button), therefor, the actionlistener still needs the variables from inside the constructor when it ran, thus they should be declared final so it's safe for the actionlistener to copy the references.

Now I understood everything :)

A variable defined locally to a method of an outer class A is not variable to the methods of an inner class B unless it is declared to be finall.


---------- Post added at 07:49 PM ---------- Previous post was at 06:49 PM ----------

As I understand, When I click the button I invoke the method (method of ActionListener )that is into the Innerclass (Inner class is into the Constructor). In the other words, When I click the button I don't invoke Constructor Class, I only invoke actionPerformed() method. dont I?
GNU/Linux Is the Best.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

VakhoQ said:



As I understand, When I click the button I invoke the method (method of ActionListener )that is into the Innerclass (Inner class is into the Constructor). In the other words, When I click the button I don't invoke Constructor Class, I only invoke actionPerformed() method. dont I?
Yes, you don't run anything from the constructor again, yet use variables from there. Hence the final.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users