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.