Results 1 to 2 of 2
- 04-08-2012, 04:29 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
Trying to do a basic calculator...
I really didn't want to have to resort to this, but I don't have a lot of resources, and what little I do have aren't really teaching me anything. It's an assignment for school. I'm not asking anyone to fill out the code for me, I just really need to understand how to go about doing something like this. I wager it's really easy, but I've been trying to wrap my head around it for more than a week now and it's already quite a bit overdue (it was due this past Monday).
I'm very new to Java, and I'm learning it in the worst way: online classes.
If you don't want to help me on the basis that it's a school assignment, that's cool, just ignore me.
Otherwise, I really need help.
I'm pretty sure I'm stuck on ActionListeners and ActionEvents.
I've tried various things to get it to work, but everything I've tried has rendered unsuccessful builds.
I don't need number buttons, just the sign buttons and then, I guess, the equal button has to follow up and do all the math work...
I guess just give me some pointers on the whole actionlistener event thingies.
Or maybe point me to a really simplified tutorial on how to get this thing, or things like it, to work...
Java Code: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 { 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 buttonMinus = new JButton(" - "); buttonPanel.add(buttonMinus); JButton buttonMulti = new JButton(" x "); buttonPanel.add(buttonMulti); JButton buttonDivi = new JButton(" / "); buttonPanel.add(buttonDivi); JButton buttonEqual = new JButton(" = "); buttonPanel.add(buttonEqual); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Basic Calculator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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); } }
Last edited by pbrockway2; 04-08-2012 at 05:12 AM. Reason: quote tags replaced with code
- 04-08-2012, 10:42 PM #2
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
Re: Trying to do a basic calculator...
Oracle has the best tutorials XD
here you go: How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
And maybe you should start with something easier? How about a frame with 1 button that prints something in the command line if you press it? That way you would understand the concept without getting lost in a spaghetti of code. :p
Im my opinion the Oracle java tutorials are the best you will find on the web!
Similar Threads
-
need help with my basic java calculator code
By hashey100 in forum New To JavaReplies: 3Last Post: 11-12-2011, 05:48 PM -
Basic Calculator
By Bimz in forum New To JavaReplies: 13Last Post: 09-09-2011, 11:47 AM -
help with basic sum calculator in java
By java157 in forum New To JavaReplies: 8Last Post: 03-18-2011, 12:12 AM -
Basic Calculator
By trtoy in forum New To JavaReplies: 1Last Post: 12-25-2009, 02:06 AM -
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06-25-2009, 01:34 AM
Bookmarks