Results 1 to 2 of 2
Thread: Basic Calculator
- 12-25-2009, 12:49 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Basic Calculator
So as you can see I am very new to this...
I am getting an error while using textpad. The error is "Desktop\BasicCalculator.java:84: illegal character: \29"
I need to add other functions but I thought I would try to run it first and it doesn't even work with just the addition. Here is thy code.
Thanks people!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BasicCalculator extends JFrame{
private static final int FRAME_WIDTH = 150;
private static final int FRAME_HEIGHT = 120;
// Keeps track of the current operation (subtract, add, etc)
private static final int NO_OPERATION = 0;
private static final int ADDITION = 1;
public static int operation = NO_OPERATION;
public static JTextField textFieldDisplay;
public static double Value1 = 0; // holds the value before the operation
public static void main(String[] args) {
// Set up the user interface
JFrame frame = new JFrame();
JPanel buttonPanel = new JPanel();
frame.add(buttonPanel);
// create two buttons, plus and equal and a text box for answers
textFieldDisplay = new JTextField(10);
buttonPanel.add(textFieldDisplay);
JButton buttonPlus = new JButton(" + ");
buttonPanel.add(buttonPlus);
JButton buttonEqual = new JButton(" = ");
buttonPanel.add(buttonEqual);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Basic Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
// called when the equal sign '=' is pressed
class EqualSignListener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
double Value2 = Double.parseDouble(textFieldDisplay.getText());
if (operation == ADDITION) {
// plus sign pressed before the equal sign
Value2 += Value1;
}
// Convert from a answer to a string
Double answer = new Double(Value2);
textFieldDisplay.setText( answer.toString() );
// Reset the operation to show no current operation
operation = NO_OPERATION;
}
}
// called when a plus sign '+' is pressed
class PlusSignListener implements ActionListener {
public void actionPerformed(ActionEvent event)
{
Value1 = Double.parseDouble(textFieldDisplay.getText());
operation = ADDITION;
}
}
// Add the methods that will be called when these buttons are pressed
ActionListener plusSignListener = new PlusSignListener();
buttonPlus.addActionListener(plusSignListener);
ActionListener equalSignListener = new EqualSignListener();
buttonEqual.addActionListener(equalSignListener);
}
}
- 12-25-2009, 01:06 AM #2
Member
- Join Date
- Dec 2009
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
need help with my calculator
By semoche in forum AWT / SwingReplies: 6Last Post: 12-04-2009, 10:16 PM -
Calculator
By water in forum AWT / SwingReplies: 4Last Post: 09-23-2009, 06:00 AM -
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06-25-2009, 12:34 AM -
help with calculator
By kalibballer in forum New To JavaReplies: 8Last Post: 04-01-2009, 12:57 PM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks