Hey Everyone!

My name is Ed and I am new to programming and your community. Thanks for the warm welcome and I hope you can follow me with my coding problems.
My code is for a project that simulates a simple vending machine. When the program is run three buttons will be displayed. They are representatives of a nickle, dime, and qarter. Everytime the user clicks on one of the buttons, the amount of that button is subtracted from the cost of a sode (75 cents). Then a message will be displayed that tells the users the remaining amount ("Please enter 40 more cents." if a dime and quarter buttons were clicked.)

So far I have written two files.
One is the main driver program.
The second file is where most of my work is.

In the VendingMachine class I have my JButtons, ButtonHandler, ActionListener, actionPerformed. It is here I am running into problems. I think everything is set up correctly for my buttons. I judt don't know how to setup the click procedures for them. How do you take the click procedure and have it register with the program to perform the task and then display a message?
Well guys, thanks for staying with me. If you have any suggestions I am all ears. Take care and hope to hear from you soon. Here is my code, I hope you can follow it. I am going to post the driver file first then the second file.

VendingMachineTest

// Driver
package simplevendingmachine;
import javax.swing.JFrame;
public class VendingMachineTest
{
    public static void main(String[] args)
    {
       VendingMachine vendingMachine = new
               VendingMachine();  // Create VendingMachine Frame.
       vendingMachine.setDefaultCloseOperation (
               JFrame.EXIT_ON_CLOSE);
       vendingMachine.setSize ( 700 , 500 );   // Sets frame size.
       vendingMachine.setVisible ( true );     // Display frame.
    } // End main.
} // End class VendingMachine

Here is my other File (VendingMachine):

// Takes the input from the three JButton 
package simplevendingmachine;

import java.awt FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JLabel;

public class VendingMachine extends JFrame
{
    private JButton nickle;  // nickle button
    private JButton dime;    // dime button
    private JButton quarter; // quarter button
    // vending machine add JButtons to JFrame
    public VendingMachine()
    {
        super ("Vending Machine");
        setLayout ( new FlowLayout() );    // seting frame layout
        nickle = new JButton("Nickle");    // Button with text
        add ( nickle );                    // Add nickle button to frame.
        dime = new JButton ("Dime");       // Button with text.
        add ( dime );                      // Add dime button to frame.
        quarter = new JButton ("Quarter"); // Button with text.
        add ( quarter );                   // Add quarter button to frame
        // Create new ButtonHandler for button handling
        ButtonHandler handler = new ButtonHandler();
        nickle.addActionListener( handler );
        dime.addActionListener ( handler );
        quarter.addActionListener ( handler );
    } // End of VendingMachine Constructor.

 // Inner class for button event handling.
    private class ButtonHandler implements ActionListener
    {
        // Handle button events.
        public void actionPerformed( ActionEvent event )
        {
            String string = "";  // Declsre string to display.
            // Process nickle button.
            if ( e.getSource() == nickle )
            // Process dime button.
            if ( e.getSource() == dime )
            // Process quarter button.
            if ( e.getSource() == quarter)
        } // End method actionPerformed.
    } //end private inner class ButtonHandler.
} // End class VendingMachine.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Will be back in about 30 minutes. Chow time.... Thanks again for taking the time to read my post.....

The class with the buttons has to implement ActionListener. I see that you've implemented the actionPerformed method, which is the method that's called when the button is clicked, but you also have to declare your class as VendingMachine extends JFrame implements ActionListener. In addition, you have to call the addActionListener method on your buttons.

Read this link for more information on ActionListeners. http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

Thank you so much.... I just got in and going to sit down and read the link you sent me. Take care and thanks for the help.

Ed

Thank you for your help. I apologize for not responding earlier, but I have been dealing with a terrible cold. I will read the article you posted and again thanks for the help.

eD

This article has been dead for over six months. Start a new discussion instead.