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 2 of 2

Thread: problem using a text feild

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

    Default problem using a text feild

    Hello, i am trying to calculate a value the set into a text feild but I Cant get it to work (the variable last). My code is included below, I just cant understand why it does not work. Look at the bottom or so 20 lines also the problem area attached below whole code.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
     
     	class Calculator extends JFrame
     	{
     	JTextField firstnumber, operator, answer, secondnumber;  /*initialises the three text feilds*/
     
     
     	JMenu copyMenu;						       		/*creates the menu*/
     	JMenuItem swap, compare; 		           		/*creates the menu options*/
     	JMenuBar bar;                              		/*creates the menu bar*/
     	JButton calculate;
     
     		private JPanel labelTextPanel(String lab, JTextField txt)
    	{
    		JPanel pan = new JPanel();
    		pan.setLayout(new FlowLayout());
    		pan.add(new JLabel(lab));
    		pan.add(txt);
    		return pan;
    	}
     
     	public Calculator()
      	{
     	setTitle("calculator");
    	setSize(400, 290);
    	setLocation(400, 300);
     
    	Container c = getContentPane();		       		/*creates the content pane, adds colour and sets a flow layout*/
    	c.setBackground(Color.blue);
    	c.setLayout(new FlowLayout());
     
    	calculate = new JButton("calculate");
      	c.add(calculate);
     
    	firstnumber = new JTextField (20);
    	c.add(firstnumber);					      		 /*creates the three text feilds on the content box and set length of 20*/
    	JPanel p1 = labelTextPanel("first number", firstnumber);
    	c.add(p1);
     
    	operator = new JTextField (20);
    	c.add(operator);
    	JPanel p2 = labelTextPanel("operator", operator); /*calls the private method that creates the labels*/
    	c.add(p2);
     
    	secondnumber = new JTextField (20);
    	c.add(secondnumber);
    	JPanel p3 = labelTextPanel("secondnumber", secondnumber);
    	c.add(p3);
     
    	answer = new JTextField (20);
    	c.add(answer);
    	JPanel p4 = labelTextPanel("answer", answer);
    	c.add(p4);
     
    	swap = new JMenuItem("Swap");			   		/*adds the two menu options and gives there names*/
    	compare = new JMenuItem("compare");
    	copyMenu = new JMenu ("Copy and Compare"); 		/*creates the menu option button on bar and gives a title*/
    	copyMenu.add(swap);
    	copyMenu.add(compare);					   		/*adds the menu options*/
     
    	bar = new JMenuBar();
    	bar.add(copyMenu);						   		/*creates the bar and adds the previous menu option to the bar*/
    	setJMenuBar(bar);
     
     
     
    	swap.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	String temp1 = firstnumber.getText();			/*swaps the contents of the text boxes*/
    	String temp2 = secondnumber.getText();
    	firstnumber.setText(temp2);
    	secondnumber.setText(temp1);
    	}
    	});
     
    	compare.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	int first;
    	int second;
     
    	String temp7 = firstnumber.getText();
    	String temp8 = secondnumber.getText();
    	first = Integer.parseInt(temp7);
    	second = Integer.parseInt(temp8);
     
    	if (first < second)
    	{
    	answer.setText("the first number is less than the second");
    	}
    	else if (first > second)
    	{
    	answer.setText("the first number is greater than the second");
    	}
    	else
    	{
    	answer.setText("they are equal");
    	}
     
    	}
    	});
     
    	calculate.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	int total;
    	int first;
    	int second;
     
     
    	String temp3 = firstnumber.getText();
    	String temp4 = secondnumber.getText();
    	String temp5 = operator.getText();
    	first = Integer.parseInt(temp3);
    	second = Integer.parseInt(temp4);
     
    	if (temp5 == "-")
    	{
    	total = first - second;
    	String last = Integer.toString(total);
     
    	}
     
    	}
    	}
    	);
     
    	setVisible(true);						   		/*makes the whole GUI visible and allows the program to be exited*/
    	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      	}
     
     	}



    	calculate.addActionListener(
    	new ActionListener()
    	{
    	public void actionPerformed(ActionEvent e)
    	{
    	int total;
    	int first;
    	int second;
     
     
    	String temp3 = firstnumber.getText();
    	String temp4 = secondnumber.getText();
    	String temp5 = operator.getText();
    	first = Integer.parseInt(temp3);
    	second = Integer.parseInt(temp4);
     
    	if (temp5 == "-")
    	{
    	total = first - second;
    	String last = Integer.toString(total);
     
    	}
     
    	}
    	}
    	);
     
    	setVisible(true);						   		/*makes the whole GUI visible and allows the program to be exited*/
    	setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      	}
     
     	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,283
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: problem using a text feild

    Cross posted at Help with my java program - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    copeg (March 22nd, 2012)

Similar Threads

  1. problem reading text file to hash map
    By cataschok in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 27th, 2012, 11:41 AM
  2. text value addition problem
    By kundan_101 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: January 8th, 2011, 11:46 AM
  3. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 05:30 AM
  4. text.length method problem?
    By computercoder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2010, 11:40 AM
  5. java. Text problem...
    By Ranger-Man in forum Java SE APIs
    Replies: 8
    Last Post: September 7th, 2009, 08:19 PM