I can't  get my program to run correctly

I made a calculator but i can't get the values to update correctly. If you run my code you'll see what I'm talking about. The value number isn't updating and I can only enter in a double digit number (using the buttons) as the initial number. I think its a problem with my boolean statement, but I've tried everything I could think of and things only get worse. (I still haven't figured out the decimal button either.)
   import java.awt.BorderLayout; 
   import java.awt.Container;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;
   import java.awt.event.WindowAdapter;
   import java.awt.event.WindowEvent;
   import javax.swing.JButton; 
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JScrollPane;
   import javax.swing.JTextArea;
   import javax.swing.JTextField;
   public class CalculatorFrame4 extends JFrame
      public CalculatorFrame4()
               JPanel calculator=new JPanel();
               mainPanel();
               pack();
          public void mainPanel()
               number=0;
               nextNum=0;
               result=0;
               operator="=";
               start=true;        
               numberField =new JTextField();
               numberField.setEditable(true);
               final JButton button1 =new JButton("1");
               final JButton button2 =new JButton("2");
               final JButton button3 =new JButton("3");
               final JButton button4 =new JButton("4");
               final JButton button5 =new JButton("5");
               final JButton button6 =new JButton("6");
               final JButton button7 =new JButton("7");
               final JButton button8 =new JButton("8");
               final JButton button9 =new JButton("9");
               final JButton button0 =new JButton("0");
               final JButton divbutton =new JButton("/");
               final JButton multbutton =new JButton("*");
               final JButton minusbutton =new JButton("-");
               final JButton plusbutton =new JButton("+");
               final JButton decbutton =new JButton(".");
               final JButton equalsbutton =new JButton("=");
               class NumberListener implements ActionListener
                    public void actionPerformed(ActionEvent event)
                         Object source =event.getSource();
                         if (start)
                              if (source == button1)
                                   numberField.setText(numberField.getText()+1);                                   
                              else if (source == button2)
                                   numberField.setText(numberField.getText()+2);
                              else if (source == button3)
                                   numberField.setText(numberField.getText()+3);
                              else if (source == button4)
                                   numberField.setText(numberField.getText()+4);
                              else if (source == button5)
                                   numberField.setText(numberField.getText()+5);
                              else if (source == button6)
                                   numberField.setText(numberField.getText()+6);
                              else if (source == button7)
                                   numberField.setText(numberField.getText()+7);
                              else if (source == button8)
                                   numberField.setText(numberField.getText()+8);
                              else if (source == button9)
                                   numberField.setText(numberField.getText()+9);
                              else if (source == button0)
                                   numberField.setText(numberField.getText()+0);
                              number=Double.parseDouble(numberField.getText());
                         else
                              numberField.setText("");
                              if (source == button1)
                                   numberField.setText(numberField.getText()+1);
                              else if (source == button2)
                                   numberField.setText(numberField.getText()+2);
                              else if (source == button3)
                                   numberField.setText(numberField.getText()+3);
                              else if (source == button4)
                                   numberField.setText(numberField.getText()+4);
                              else if (source == button5)
                                   numberField.setText(numberField.getText()+5);
                              else if (source == button6)
                                   numberField.setText(numberField.getText()+6);
                              else if (source == button7)
                                   numberField.setText(numberField.getText()+7);
                              else if (source == button8)
                                   numberField.setText(numberField.getText()+8);
                              else if (source == button9)
                                   numberField.setText(numberField.getText()+9);
                              else if (source == button0)
                                   numberField.setText(numberField.getText()+0);
                              nextNum=Double.parseDouble(numberField.getText());
               ActionListener listener =new NumberListener();
               button1.addActionListener(listener);
               button2.addActionListener(listener);
               button3.addActionListener(listener);
               button4.addActionListener(listener);
               button5.addActionListener(listener);
               button6.addActionListener(listener);
               button7.addActionListener(listener);
               button8.addActionListener(listener);
               button9.addActionListener(listener);
               button0.addActionListener(listener);           
               class CalculateListener implements ActionListener
                    public void actionPerformed(ActionEvent event)
                         numberField.setText("");
                         Object source=event.getSource();
                         if (source==divbutton)
                              operator="/";
                         else if (source==multbutton)
                              operator="*";
                         else if (source==plusbutton)
                              operator="+";
                         else if (source==minusbutton)
                              operator="-";
                         else if (source==equalsbutton)     
                              operate(nextNum);
                              numberField.setText(""+number);
                         start=false;
               ActionListener calclistener =new CalculateListener();
               divbutton.addActionListener(calclistener);
               multbutton.addActionListener(calclistener);
               plusbutton.addActionListener(calclistener);
               minusbutton.addActionListener(calclistener);
               equalsbutton.addActionListener(calclistener);
               JPanel controlPanel = new JPanel();
               controlPanel.setLayout(new GridLayout(4, 4));
             controlPanel.add(numberField);
         controlPanel.add(button7);
         controlPanel.add(button8);
         controlPanel.add(button9);
               controlPanel.add(divbutton);
           controlPanel.add(button4);
         controlPanel.add(button5);
         controlPanel.add(button6);
               controlPanel.add(multbutton);
               controlPanel.add(button1);
         controlPanel.add(button2);
         controlPanel.add(button3);
               controlPanel.add(minusbutton);
               controlPanel.add(decbutton);
         controlPanel.add(button0);
         controlPanel.add(equalsbutton);
               controlPanel.add(plusbutton);
         JPanel clearPanel = new JPanel();
               final JButton clearbutton =new JButton("CLEAR");
         clearPanel.add(clearbutton);
               class ClearListener implements ActionListener
                    public void actionPerformed(ActionEvent event)
                         number=0;
                         nextNum=0;
                         numberField.setText("");
             ActionListener alistener =new ClearListener();
               clearbutton.addActionListener(alistener);
               getContentPane().add(numberField, BorderLayout.NORTH);
         getContentPane().add(controlPanel, BorderLayout.CENTER);
         getContentPane().add(clearPanel, BorderLayout.SOUTH);
          public void operate(double otherNum)
               if (operator.equals("/"))
                    number = number/otherNum;
               else if (operator.equals("*"))
                    number = number*otherNum;                                
               else if (operator.equals("+"))
                    number = number+otherNum;
               else if (operator.equals("-"))
                    number = number-otherNum;
          private boolean start;
          private double number;
          private double nextNum;
          private double result;
          private String operator;
          private JTextField numberField;
     }Here's the test program
   import javax.swing.JFrame;
   public class CalculatorTest
      public static void main(String[] args)
         JFrame frame = new CalculatorFrame4();
         frame.setTitle("Calculator");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.show();
   }

I am no expert, in fact I am a very newbie. But I was trying to get my code to update too, and I didn't know I had to call the validate() function...try doing that, if you haven't already, also read up on validate() from the Component (or Container) API. I didn't really look through your code, I wouldn't be able to keep it in my head anyway.

Similar Messages

  • Extra-Beginner Question:  Can you get a program to output PrintLn?

    In other words, can I get a program to output its status in real-time, like this:
    http://img269.imageshack.us/img269/4354/outputexample.png
    I know you can do this with Netbeans, but my thin client doesn't have enough room for it. Is there a simple java utility that does the same thing?
    Thanks for reading.

    Netbeans just runs (Apache) Ant (3 MB or so if you don't include the documentation) so you can can try to see if that fits and run it from the command line. If it doesn't you'll have to run javac/java from the command line yourself.

  • How can I get quicktime pro 7 running

    I bought quicktime pro 7 a couple of years ago and have a registration code.  At the time I used the program to make some time lapse movies.  I'd like to make a couple of time lapse movies again but quicktime pro 7 is no where to be found.  This probably ha something to do with updating the os between then and now.  The question is how can I get quicktime pro 7 running again?  The unlocking instructions I have for quicktime pro 7 that I received with the key do not correspond with what the system preferences now show.  Any assistance would be appreciated.  I am currently running OS X version 10.9.5 on an older iMac.

    Open the QuickTime 7 player(if it’s not installed, click here), choose Registration from the QuickTime Player 7 menu, and supply your key there.
    (115130)

  • How can I get my games to run smooth with apple tv. I'm getting jerky movement

    How can I get my games to run smooth with apple tv?

    Check your network for interference (www.istumbler.net, check router manual in regards to changing channels) and other activities possibly running in the background.

  • How can I get an mp4 video running on iPad Mini?

    How can I get an mp4 video running on iPad Mini, which is already in the iTunes File?

    Pad2, the new iPad Supported Video Formats & Movie Formats
    H.264 video up to 1080p, 30 frames per second, High Profile level 4.1 with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats;
    MPEG-4 video up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps per channel, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats;
    Motion JPEG (M-JPEG) up to 35 Mbps, 1280 by 720 pixels, 30 frames per second, audio in ulaw, PCM stereo audio in .avi file format
    You can use a USB flash drive & the camera connection kit.
    Plug the USB flash drive into your computer & create a new folder titled DCIM. Then put your movie/photo files into the folder. The files must have a filename with exactly 8 characters long (no spaces) plus the file extension (i.e., my-movie.mov; DSCN0164.jpg).
    Now plug the flash drive into the iPad using the camera connection kit. Open the Photos app, the movie/photo files should appear & you can import. (You can not export using the camera connection kit.)
     Cheers, Tom

  • I would like to read a text file in which the decimal numbers are using dots instead of commas. Is there a way of converting this in labVIEW, or how can I get the program to enterpret the figures in the correct way?

    The program doest enterpret my figures from the text file in the correct way since the numbers contain dots instead of commas. Is there a way to fix this in labVIEW, or do I have to change the files before reading them in the program? Thanks beforehend!

    You must go in the labview option menu, you can select 'use the local
    separator' in the front side submenu (LV6i).
    If you use the "From Exponential/Fract/Eng" vi, you are able to select this
    opton (with a boolean) without changing the labview parameters.
    (sorry for my english)
    Lange Jerome
    FRANCE
    "Nina" a ecrit dans le message news:
    [email protected]..
    > I would like to read a text file in which the decimal numbers are
    > using dots instead of commas. Is there a way of converting this in
    > labVIEW, or how can I get the program to enterpret the figures in the
    > correct way?
    >
    > The program doest enterpret my figures from the text file in the
    > correct way since the numbers contain dots instea
    d of commas. Is there
    > a way to fix this in labVIEW, or do I have to change the files before
    > reading them in the program? Thanks beforehend!

  • I purchased Photoshop cs5 a while back in disk form for my old imac. Now my new laptop doesnt have a disc drive... can I get the program onto my new computer? I don't have the box, but have the old computer it's on...

    I purchased Photoshop cs5 a while back in disk form for my old imac. Now my new laptop doesnt have a disc drive... can I get the program onto my new computer? I don't have the box, but have the old computer it's on...
    also had a friend place inD and Ai on my old laptop and am curious to know how to get it to my new one

    As long as you have your serial number, you may download and install
    OLDER previous versions http://www.adobe.com/downloads/other-downloads.html

  • How can I get my macbook pro running Lion to print on my HP photosmart 309a??

    how can I get my macbook pro running Lion to print on my HP photosmart 309a??

    Did you install the drivers? Or have Software Update install them? This printer is supported:
    http://support.apple.com/kb/HT3669

  • HT4410 If I install Windows 7 on my MacBook Pro using Bootcamp, now having 2 start up drives, do I select the Windows drive, then I can install a software program that runs only on a PC?

    If I install Windows 7 on my MacBook Pro using Bootcamp, now having 2 start up drives, do I select the Windows drive, then I can install a software program that runs only on a PC?

    Thank you

  • Computer Crashed with CS4 - How can I get these programs to my new computer?

    Computer Crashed with CS4 - How can I get these programs to my new computer?

    You will want to install them from the installation media.

  • How can I get this program to work -- Adobe Send or Adobe SendNow

    How can I get this program to work?  I have used Adobe SendNow successfully for several years and am on automatic renewal and getting nothing but frustration for my money!  I have been unsuccessful at sending anything in the past several months.  Today while working on a deadline and many unsuccessful attempts to upload files, I signed up for a free trial at a competitor in order to get my files sent.  Want a refund  -- or better yet, keep my money and get it to work right again.

    You can't.
    Sounds like it he gave you a stolen iPhone or at least a worthless one.

  • I have a I-mac that is not able to upgrade the os past 10.7.5, when I go to upgrade Pages it says i have to be running os 10.10, how can I get Pages that will run with os 10.7.5.

    I have a I-mac that is not able to upgrade the os past 10.7.5, when I go to upgrade Pages it says i have to be running os 10.10, how can I get Pages that will run with os 10.7.5.

    Click here and follow the instructions. If they’re not applicable, buy an iWork 09 DVD from a source such as Amazon or eBay.
    (116937)

  • I just got an IPAD2 on Saturday.  With the Mac, I could always tell when programs were open/running by going to the menu bar.  How can I tell what programs are running in the background.  I am struggling with not being able to go to a menu bar at the top

    I just got an IPAD2 on Saturday.  With the Mac, I could always tell when programs were open/running by going to the menu bar.  How can I tell what programs are running in the background.  I am struggling with not being able to go to a menu bar at the top

    You can see which apps are open and/or recently used by double-clicking the home button to bring up the taskbar at the bottom of the screen (not all apps that appear there are active) - you can then close apps on it by pressing and holding one of them for a couple of seconds or so until they start shaking, then press the '-' in the top left of the Mail app to close it, and touch any part of the screen above the taskbar so as to stop the shaking and close the taskbar. If you have an app 'open' on-screen then it won't appear on the taskbar, you will need to go back to the homescreen first, and it should then show when you next open the taskbar.
    If you haven't already got a copy then you can download the iPad manual from here : http://support.apple.com/manuals/#ipad

  • How can I tell if programs are running in the background.

    iPod touch version 4.2.1, how can I tell if programs are running in the background.  Battery is dead quickly and time/date reverts to Dec 31, 1969.  can see thin silver frame around screen like something is running in background.

    You really can't tell unless you hear music or similar. Many are always listening for notifications and other things.
    To preserve battery life see the following:
    Apple - Batteries - iPod
    I leave mine in airplane mode when sleeping.

  • I am trying to burn a .mov file from fcp xpress timeline to a dvd.  The popup message I receive prior to inserting the dvd is that I need 20.05 gb of space available on dvd (a 1.25 min). How can I get this program onto a dvd that shows a max of 8.5gb?.

    I am trying to burn a .mov file from fcp xpress timeline to a dvd.  The popup message I receive prior to inserting the dvd is that I need 20.05 gb of space available on dvd (a 1.25 min program). How can I get this program onto a dvd (where the max available gb's  for off the shelf dvds-r)  shows a max of 8.5gb?.  Please help.  Thank You!

    You have posted your question in the Final Cut  Pro X forum. You want to be in this forum instead; https://discussions.apple.com/community/professional_applications/final_cut_expr ess_hd
    Good luck.
    Russ

Maybe you are looking for

  • BI related Knowledge management question

    Hi Gurus, i am a BI consultat, new to knowledge management, I need some documents trlated to below mentioned topics *Content Management* Using KM as repository for Queries,(web and Excel), Workbooks, Web Templates. Publishing background report output

  • FAGLF101 Understanding the track

    Hi Experts I have posted used tcode FAGLF101 to transfer the debit balances of Vendor at a gicen date to Vendor Recievable and Vendor Recievable Adjustment Account in order to make fair presentation of reports. Let us say only one Vendor X&Y has nega

  • Passing internal tables into smartforms

    Hi All, I am testing a smartform for PO. But the smartform is already designed and it has two internal tables of type EKKO and EKPO. When i run the smartform its displaying the layout correctly but with no data ,which is true as the two internal tabl

  • Google Contact Sync, causing randon duplicates.ugh!  Thoughts, Suggestions?

    Hey guys, So I just discovered the new Google Contact sync in Address Book, and also own an iPhone. But so far, I have had MAJOR issues with this, and I've been struggling with this issue since last week. Luckily Time Machine has been saving my day.

  • Some of space designer presets are missing, what should I do?

    Hi! Some of space designer presets are missing, what should I do? FCPX 10.1.3 Yosemite Mac Pro end 2013