i have tried every thing to get image on my button, i have looked all over the internet but i can't get a image to display, if any one can please help me.
import static javax.swing.JOptionPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
//Don's carpark2.java was used in making this vending machine.
public class CarPark2 extends JFrame implements ActionListener {
JButton twoP,fiveP, tenP, twentyP, fiftyP, onePound, twoPounds, Yorkie, creamegg, chrisp, curlywurly, Cearialbar;
JLabel messLabel = new JLabel("Amount to pay: ");
JTextField message = new JTextField(10);
JLabel pickedlab = new JLabel("You picked: ");
JTextField picked = new JTextField(10);
JLabel product = new JLabel("Amount to pay: ");
JTextField product1 = new JTextField(10);
double amount = 0; // payment in pounds
DecimalFormat pounds = new DecimalFormat("£0.00");
public static void main(String[] args) {
CarPark2 c = new CarPark2();
c.setTitle("Vending Machine");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(800, 500);
c.setVisible(true);
}
CarPark2() {
//items on left
setLayout(new BorderLayout());
twoP = new JButton("2p");
fiveP = new JButton("5p");
tenP = new JButton("10p");
twentyP = new JButton("20p");
fiftyP = new JButton("50p");
onePound = new JButton("£1");
twoPounds = new JButton("£2");
//item on right
Yorkie = new JButton("Yorkie");
creamegg = new JButton("Cream Egg");
chrisp = new JButton("Chrisp");
curlywurly = new JButton("Curly wurly");
Cearialbar = new JButton("Cearial Bar");
//listenen to buttons
twoP.addActionListener(this);
fiveP.addActionListener(this);
tenP.addActionListener(this);
twentyP.addActionListener(this);
fiftyP.addActionListener(this);
onePound.addActionListener(this);
twoPounds.addActionListener(this);
Yorkie.addActionListener(this);
creamegg.addActionListener(this);
chrisp.addActionListener(this);
curlywurly.addActionListener(this);
Cearialbar.addActionListener(this);
//add to right
rightButtons();
JPanel leftSide = new JPanel();
leftSide.setLayout(new GridLayout(4, 2000));
leftSide.add(twoP);
leftSide.add(fiveP);
leftSide.add(tenP);
leftSide.add(twentyP);
leftSide.add(fiftyP);
leftSide.add(onePound);
leftSide.add(twoPounds);
//add to left side
add("West", leftSide);
JPanel rightSide = new JPanel();
rightSide.setLayout(new GridLayout(4, 2000));
rightSide.add(Yorkie);
rightSide.add(creamegg);
rightSide.add(chrisp);
rightSide.add(curlywurly);
rightSide.add(Cearialbar);
add("East", rightSide);
//center
JPanel middle = new JPanel();
middle.setLayout(new FlowLayout());
middle.add(messLabel);
message.setEditable(false);
middle.add(message);
middle.add(pickedlab);
picked.setEditable(false);
middle.add(picked);
add("Center", middle);
}
//cost of things
public void actionPerformed(ActionEvent e) {
//if button pressed than display and pay
if (e.getSource() == Yorkie) { amount = 0.65; leftButtons(); picked.setText("Yorkie"); showMessageDialog(this, "121 Kcal Per Serving","", JOptionPane.INFORMATION_MESSAGE);}
if (e.getSource() == creamegg) { amount = 0.45; leftButtons(); picked.setText("Cream Egg");showMessageDialog(this, "174 Kcal Per Serving","", JOptionPane.INFORMATION_MESSAGE);}
if (e.getSource() == chrisp) { amount = 0.80; leftButtons();picked.setText("Chrisp"); showMessageDialog(this, "130 Kcal Serving","", JOptionPane.INFORMATION_MESSAGE);}
if (e.getSource() == curlywurly) { amount = 0.70; leftButtons(); picked.setText("Curly Wurly");showMessageDialog(this, "117 Kcal Per Serving","", JOptionPane.INFORMATION_MESSAGE);}
if (e.getSource() == Cearialbar) { amount = 0.60; leftButtons(); picked.setText("Cearial Bar");showMessageDialog(this, "124 Kcal Per Serving","", JOptionPane.INFORMATION_MESSAGE);}
//if button pressed than display
if (e.getSource() == twoP) amount -= 0.02;
if (e.getSource() == fiveP) amount -= 0.05;
if (e.getSource() == tenP) amount -= 0.10;
if (e.getSource() == twentyP) amount -= 0.20;
if (e.getSource() == fiftyP) amount -= 0.50;
if (e.getSource() == onePound) amount -= 1;
if (e.getSource() == twoPounds) amount -= 2;
System.out.println(amount);
if (amount > 0) message.setText(pounds.format(amount));
else {
message.setText("");
if (amount < 0) {
double change = -amount;
showMessageDialog(this, "Your change is "
+ pounds.format(change)
+ coins(change),
"Change", JOptionPane.INFORMATION_MESSAGE);
} else {
showMessageDialog(this, "Thank you",
"Exact amount", JOptionPane.INFORMATION_MESSAGE);
}
rightButtons();
}
}
// pick 1st black out right buttons
void leftButtons() {
twoP.setEnabled(true);
fiveP.setEnabled(true);
tenP.setEnabled(true);
twentyP.setEnabled(true);
fiftyP.setEnabled(true);
onePound.setEnabled(true);
twoPounds.setEnabled(true);
Yorkie.setEnabled(false);
creamegg.setEnabled(false);
chrisp.setEnabled(false);
curlywurly.setEnabled(false);
Cearialbar.setEnabled(false);
}
// black out left buttons
void rightButtons() {
twoP.setEnabled(false);
fiveP.setEnabled(false);
tenP.setEnabled(false);
twentyP.setEnabled(false);
fiftyP.setEnabled(false);
onePound.setEnabled(false);
twoPounds.setEnabled(false);
Yorkie.setEnabled(true);
creamegg.setEnabled(true);
chrisp.setEnabled(true);
curlywurly.setEnabled(true);
Cearialbar.setEnabled(true);
}
//change given
String coins(double change) {
String answer = ":";
if (change >= 1) {
answer += "\nOne £1 coin";
change -= 1;
}
if (change >= 0.50) {
answer += "\nOne 50p coin";
change -= 0.50;
}
if (change >= 0.40) {
answer += "\nTwo 20p coins";
change -= 0.40;
}
if (change >= 0.20) {
answer += "\nOne 20p coin";
change -= 0.20;
}
if (change >= 0.10) {
answer += "\nOne 10p coin";
change -= 0.10;
}
if (change >= 0.05) {
answer += "\nOne 5p coin";
change -= 0.05;
}
if (change >= 0.02) {
answer += "\nOne 2p coin";
change -= 0.02;
}
return answer;
}
}
Thanks