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

Thread: Another Calculator Problem

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Another Calculator Problem

    EDIT: Nevermind, solved it.


    Hi guys, another problem with my calculator program has arose. I have added a button which will make the input positive or negative depending on what it is (I have also added many other things). To make something positive from negative, I delete the first character (assuming it should be a -). The problem is, sometimes it deletes the first character when it is suppose to put a negative in front of the input. It seems random when it does this. For example, if you click the buttons in this order: 3, +/-, +, 6, +/- the 6 will be deleted instead of getting a - sign in front of it.

    Here is a runnable code:
    package JFrameClaculator;
     
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
     
    import javax.swing.AbstractButton;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.SpringLayout;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
     
    import layout.testing2;
     
    public class Calculator extends JPanel implements ActionListener{
     
    	public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    	static String textop;
    	static String textop1;
    	static int x;
    	static int x1;
    	static int x2;
    	static int x3;
    	static int y = 1;
    	public static final char LF = '\n';
    	public static final char CR = '\r';
    	static JTextField textField;
    	protected static JButton b1;
    	protected static JButton b2;
    	protected static JButton b3;
    	protected static JButton b4;
    	protected static JButton b5;
    	protected static JButton b6;
    	protected static JButton b7;
    	protected static JButton b8;
    	protected static JButton b9;
    	protected static JButton b10;
    	protected static JButton b11;
    	protected static JButton b12;
    	protected static JButton b13;
    	protected static JButton b14;
    	protected static JButton b15;
    	protected static JButton b16;
    	protected static JButton b17;
    	protected static JButton b18;
    	private static void createAndShowGUI() {
    		JFrame frame = new JFrame("Calculator v2.1");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
            String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
            try {
    			UIManager.setLookAndFeel(lookAndFeel);
    		} catch (ClassNotFoundException e) {
    			String ErrorMessage = "Error: "+e; 
    			JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    			scrollPane.setPreferredSize(new Dimension(100, 50));  
    			Object message = scrollPane;  
    			JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    			try {
    				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			} catch (ClassNotFoundException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (InstantiationException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (IllegalAccessException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (UnsupportedLookAndFeelException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			}
    		} catch (InstantiationException e) {
    			String ErrorMessage = "Error: "+e; 
    			JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    			scrollPane.setPreferredSize(new Dimension(100, 50));  
    			Object message = scrollPane;  
    			JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    			try {
    				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			} catch (ClassNotFoundException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (InstantiationException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (IllegalAccessException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (UnsupportedLookAndFeelException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			}
    		} catch (IllegalAccessException e) {
    			String ErrorMessage = "Error: "+e; 
    			JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    			scrollPane.setPreferredSize(new Dimension(100, 50));  
    			Object message = scrollPane;  
    			JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    			try {
    				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			} catch (ClassNotFoundException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (InstantiationException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (IllegalAccessException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (UnsupportedLookAndFeelException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			}
    		} catch (UnsupportedLookAndFeelException e) {
    			String ErrorMessage = "Error: "+e; 
    			JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    			scrollPane.setPreferredSize(new Dimension(100, 50));  
    			Object message = scrollPane;  
    			JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    			try {
    				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    			} catch (ClassNotFoundException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (InstantiationException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (IllegalAccessException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			} catch (UnsupportedLookAndFeelException e1) {
    				String ErrorMessage1 = "Error: "+e1; 
    				JScrollPane scrollPane1 = new JScrollPane(new JLabel(ErrorMessage1));  
    				scrollPane1.setPreferredSize(new Dimension(100, 50));  
    				Object message1 = scrollPane1;  
    				JOptionPane.showMessageDialog(null, message1, "Error", JOptionPane.ERROR_MESSAGE);      
    				System.exit(0);
    			}
    		}
            textField = new JTextField("", 23);
            textField.setEditable(false);
            textField.setBackground(Color.white);
            Font f = new Font("+/-", Font.PLAIN, 11);
            b1 = new JButton("1");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.addActionListener(new Calculator());
            b1.setActionCommand("1");
            b1.setForeground(Color.blue);
            b2 = new JButton("2");
            b2.setVerticalTextPosition(AbstractButton.CENTER);
            b2.setHorizontalTextPosition(AbstractButton.LEADING);
            b2.addActionListener(new Calculator());
            b2.setActionCommand("2");
            b2.setForeground(Color.blue);
            b3 = new JButton("3");
            b3.setVerticalTextPosition(AbstractButton.CENTER);
            b3.setHorizontalTextPosition(AbstractButton.LEADING);
            b3.addActionListener(new Calculator());
            b3.setActionCommand("3");
            b3.setForeground(Color.blue);
            b4 = new JButton("4");
            b4.setVerticalTextPosition(AbstractButton.CENTER);
            b4.setHorizontalTextPosition(AbstractButton.LEADING);
            b4.addActionListener(new Calculator());
            b4.setActionCommand("4");
            b4.setForeground(Color.blue);
            b5 = new JButton("5");
            b5.setVerticalTextPosition(AbstractButton.CENTER);
            b5.setHorizontalTextPosition(AbstractButton.LEADING);
            b5.addActionListener(new Calculator());
            b5.setActionCommand("5");
            b5.setForeground(Color.blue);
            b6 = new JButton("6");
            b6.setVerticalTextPosition(AbstractButton.CENTER);
            b6.setHorizontalTextPosition(AbstractButton.LEADING);
            b6.addActionListener(new Calculator());
            b6.setActionCommand("6");
            b6.setForeground(Color.blue);
            b7 = new JButton("7");
            b7.setVerticalTextPosition(AbstractButton.CENTER);
            b7.setHorizontalTextPosition(AbstractButton.LEADING);
            b7.addActionListener(new Calculator());
            b7.setActionCommand("7");
            b7.setForeground(Color.blue);
            b8 = new JButton("8");
            b8.setVerticalTextPosition(AbstractButton.CENTER);
            b8.setHorizontalTextPosition(AbstractButton.LEADING);
            b8.addActionListener(new Calculator());
            b8.setActionCommand("8");
            b8.setForeground(Color.blue);
            b9 = new JButton("9");
            b9.setVerticalTextPosition(AbstractButton.CENTER);
            b9.setHorizontalTextPosition(AbstractButton.LEADING);
            b9.addActionListener(new Calculator());
            b9.setActionCommand("9");
            b9.setForeground(Color.blue);
            b10 = new JButton("0");
            b10.setVerticalTextPosition(AbstractButton.CENTER);
            b10.setHorizontalTextPosition(AbstractButton.LEADING);
            b10.addActionListener(new Calculator());
            b10.setActionCommand("0");
            b10.setForeground(Color.blue);
            b11 = new JButton("+");
            b11.setVerticalTextPosition(AbstractButton.CENTER);
            b11.setHorizontalTextPosition(AbstractButton.LEADING);
            b11.addActionListener(new Calculator());
            b11.setActionCommand("+");
            b11.setForeground(Color.red);
            b12 = new JButton("-");
            b12.setVerticalTextPosition(AbstractButton.CENTER);
            b12.setHorizontalTextPosition(AbstractButton.LEADING);
            b12.addActionListener(new Calculator());
            b12.setActionCommand("-");
            b12.setForeground(Color.red);
            b13 = new JButton("x");
            b13.setVerticalTextPosition(AbstractButton.CENTER);
            b13.setHorizontalTextPosition(AbstractButton.LEADING);
            b13.addActionListener(new Calculator());
            b13.setActionCommand("x");
            b13.setForeground(Color.red);
            b14 = new JButton("/");
            b14.setVerticalTextPosition(AbstractButton.CENTER);
            b14.setHorizontalTextPosition(AbstractButton.LEADING);
            b14.addActionListener(new Calculator());
            b14.setActionCommand("/");
            b14.setForeground(Color.red);
            b15 = new JButton("C");
            b15.setVerticalTextPosition(AbstractButton.CENTER);
            b15.setHorizontalTextPosition(AbstractButton.LEADING);
            b15.addActionListener(new Calculator());
            b15.setActionCommand("C");
            b15.setForeground(Color.red);
            b16 = new JButton("=");
            b16.setVerticalTextPosition(AbstractButton.CENTER);
            b16.setHorizontalTextPosition(AbstractButton.LEADING);
            b16.addActionListener(new Calculator());
            b16.setActionCommand("=");
            b16.setForeground(Color.red);
            b17 = new JButton(".");
            b17.setVerticalTextPosition(AbstractButton.CENTER);
            b17.setHorizontalTextPosition(AbstractButton.LEADING);
            b17.addActionListener(new Calculator());
            b17.setActionCommand(".");
            b17.setForeground(Color.blue);
            b18 = new JButton("+/-");
            b18.setVerticalTextPosition(AbstractButton.CENTER);
            b18.setHorizontalTextPosition(AbstractButton.LEADING);
            b18.addActionListener(new Calculator());
            b18.setActionCommand("+/-");
            b18.setFont(f);
            b18.setForeground(Color.blue);
            contentPane.setBackground(new Color(240, 240, 240));
            contentPane.add(textField);
            contentPane.add(b1);
            contentPane.add(b2);
            contentPane.add(b3);
            contentPane.add(b4);
            contentPane.add(b5);
            contentPane.add(b6);
            contentPane.add(b7);
            contentPane.add(b8);
            contentPane.add(b9);
            contentPane.add(b10);
            contentPane.add(b11);
            contentPane.add(b12);
            contentPane.add(b13);
            contentPane.add(b14);
            contentPane.add(b15);
            contentPane.add(b16);
            contentPane.add(b17);
            contentPane.add(b18);
            b1.setPreferredSize(new Dimension(45, 30));
            b2.setPreferredSize(new Dimension(45, 30));
            b3.setPreferredSize(new Dimension(45, 30));
            b4.setPreferredSize(new Dimension(45, 30));
            b5.setPreferredSize(new Dimension(45, 30));
            b6.setPreferredSize(new Dimension(45, 30));
            b7.setPreferredSize(new Dimension(45, 30));
            b8.setPreferredSize(new Dimension(45, 30));
            b9.setPreferredSize(new Dimension(45, 30));
            b10.setPreferredSize(new Dimension(45, 30));
            b11.setPreferredSize(new Dimension(45, 30));
            b12.setPreferredSize(new Dimension(45, 30));
            b13.setPreferredSize(new Dimension(45, 30));
            b14.setPreferredSize(new Dimension(45, 30));
            b15.setPreferredSize(new Dimension(45, 30));
            b16.setPreferredSize(new Dimension(145, 30));
            b17.setPreferredSize(new Dimension(45, 30));
            b18.setPreferredSize(new Dimension(45, 30));
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, textField,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, textField,
                                 10,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b1,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b1,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b2,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b2,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b3,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b3,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b4,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b4,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b5,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b5,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b6,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b6,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b7,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b7,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b8,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b8,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b9,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b9,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b10,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b10,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b11,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b11,
                                 40,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b12,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b12,
                                 75,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b13,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b13,
                                 110,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b14,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b14,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b15,
                                 130,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b15,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b16,
                                 30,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b16,
                                 180,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b17,
                                 80,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b17,
                                 145,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
          //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, b18,
                                 180,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, b18,
                                 180,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
            frame.pack();
            frame.setSize(263, 259);
            frame.setLocation(20, 20);
            frame.setVisible(true);
            frame.setResizable(false);
    	}
    	public void actionPerformed(ActionEvent e) {
    		if (e.getSource()==b1) {
    			String text = textField.getText();
    			textField.setText(text+"1");
    		}
    		if (e.getSource()==b2) {
    			String text = textField.getText();
    			textField.setText(text+"2");
    		}
    		if (e.getSource()==b3) {
    			String text = textField.getText();
    			textField.setText(text+"3");
    		}
    		if (e.getSource()==b4) {
    			String text = textField.getText();
    			textField.setText(text+"4");
    		}
    		if (e.getSource()==b5) {
    			String text = textField.getText();
    			textField.setText(text+"5");
    		}
    		if (e.getSource()==b6) {
    			String text = textField.getText();
    			textField.setText(text+"6");
    		}
    		if (e.getSource()==b7) {
    			String text = textField.getText();
    			textField.setText(text+"7");
    		}
    		if (e.getSource()==b8) {
    			String text = textField.getText();
    			textField.setText(text+"8");
    		}
    		if (e.getSource()==b9) {
    			String text = textField.getText();
    			textField.setText(text+"9");
    		}
    		if (e.getSource()==b10) {
    			String text = textField.getText();
    			textField.setText(text+"0");
    		}
    		if (e.getSource()==b11) {
    			textop = textField.getText();
    			textField.setText("");
    			x = x + 1;
    		}
    		if (e.getSource()==b12) {
    			textop = textField.getText();
    			textField.setText("");
    			x1 = x1 + 1;
    		}
    		if (e.getSource()==b13) {
    			textop = textField.getText();
    			textField.setText("");
    			x2 = x2 + 1;
    		}
    		if (e.getSource()==b14) {
    			textop = textField.getText();
    			textField.setText("");
    			x3 = x3 + 1;
    		}
    		if (e.getSource()==b15) {
    			String str = textField.getText();
    			if (str == null) {
     
    		      }
    		      int strLen = str.length();
    		      if(strLen != 0){
    		      if (strLen < 2) {
    		          textField.setText("");
    		      }
    		      int lastIdx = strLen - 1;
    		      String ret = str.substring(0, lastIdx);
    		      char last = str.charAt(lastIdx);
    		      if (last == LF) {
    		          if (ret.charAt(lastIdx - 1) == CR) {
    		              textField.setText(ret.substring(0, lastIdx - 1));
    		          }
    		      }
    		      textField.setText(ret);
    			}
    		}
    		if (e.getSource()==b16) {
    			try{
    			if(x % 2 != 0)
    			{
    				textop1 = textField.getText();
    				float op = Float.valueOf(textop).floatValue();
    				float op1 = Float.valueOf(textop1).floatValue();
    				float opFinal = op+op1;
    				String Final = Float.toString(opFinal);
    				textField.setText(Final);
    				x = x + 1;
    			}
    			if(x1 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				float op = Float.valueOf(textop).floatValue();
    				float op1 = Float.valueOf(textop1).floatValue();
    				float opFinal = op-op1;
    				String Final = Float.toString(opFinal);
    				textField.setText(Final);
    				x1 = x1 + 1;
    			}
    			if(x2 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				float op = Float.valueOf(textop).floatValue();
    				float op1 = Float.valueOf(textop1).floatValue();
    				float opFinal = op*op1;
    				String Final = Float.toString(opFinal);
    				textField.setText(Final);
    				x2 = x2 + 1;
    			}
    			if(x3 % 2 != 0)
    			{
    				textop1 = textField.getText();
    				float op = Float.valueOf(textop).floatValue();
    				float op1 = Float.valueOf(textop1).floatValue();
    				float opFinal = op/op1;
    				String Final = Float.toString(opFinal);
    				textField.setText(Final);
    				x3 = x3 + 1;
    			}
    			else
    			{
     
    			}
    			}catch(Exception e1){
    				String ErrorMessage = "Error: "+e1; 
    				JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    				scrollPane.setPreferredSize(new Dimension(100, 50));  
    				Object message = scrollPane;  
    				JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    				textField.setText("");
    				x = 0;
    				x1 = 0;
    				x2 = 0;
    				x3 = 0;
    				y = 1;
    			}
    		}
    		if (e.getSource()==b17) {
    			String text = textField.getText();
    			textField.setText(text+".");
    		}
    		if (e.getSource()==b18) {
    			try{
    			if(y % 2 != 0)
    			{
    				String text = textField.getText();
    				textField.setText("-"+text);
    				y = y + 1;
    			}
    			else if(y % 2 == 0)
    			{
    				String text = textField.getText();
    				int pos = text.length()-text.length(); 
    		        String newValue  = text.substring(0,pos) + text.substring(pos+1);
    		        textField.setText(newValue);
    		        y = y + 1;
    			}
    			}catch(Exception e2)
    			{
    				String ErrorMessage = "Error: "+e2; 
    				JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    				scrollPane.setPreferredSize(new Dimension(100, 50));  
    				Object message = scrollPane;  
    				JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    				textField.setText("");
    				x = 0;
    				x1 = 0;
    				x2 = 0;
    				x3 = 0;
    				y = 1;
    			}
    		}
    	}
     
    }

    Here is what I think the relevant code is:
    //y starts out as 1
    if (e.getSource()==b18) {
    			try{
    			if(y % 2 != 0)
    			{
    				String text = textField.getText();
    				textField.setText("-"+text);
    				y = y + 1;
    			}
    			else if(y % 2 == 0)
    			{
    				String text = textField.getText();
    				int pos = text.length()-text.length(); 
    		        String newValue  = text.substring(0,pos) + text.substring(pos+1);
    		        textField.setText(newValue);
    		        y = y + 1;
    			}
    			}catch(Exception e2)
    			{
    				String ErrorMessage = "Error: "+e2; 
    				JScrollPane scrollPane = new JScrollPane(new JLabel(ErrorMessage));  
    				scrollPane.setPreferredSize(new Dimension(100, 50));  
    				Object message = scrollPane;  
    				JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);      
    				textField.setText("");
    				x = 0;
    				x1 = 0;
    				x2 = 0;
    				x3 = 0;
    				y = 1;
    			}
    Last edited by The_Mexican; December 4th, 2010 at 06:14 PM.


Similar Threads

  1. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM
  2. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  3. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM
  4. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  5. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM