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