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

Recommended Answers

All 7 Replies

I believe it something like this:

buttonName.setIcon(new ImageIcon (getClass().getResource("location of the image") ));

ImageIcon is under javax.Swing if you haven't import it yet.

i couldn't seem to get it to work, if anyone has any solutions i would really appreciate it.
Thanks :)

every time i get
exception in thred "main" java. lang. NullPointerException

Post your code then, because the code you posted above does not have a single line that attempts to set the image and we can't read over your shoulder here.

that would make sense, i have tried everything but it just don't seem to work, i am new to java and don't get much of it but i am having real problems adding images.

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());
		ImageIcon ic = new ImageIcon("toffee.gif");
	JButton twoP = new JButton("2P", ic);
		 
		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;
    }
}

thns you for every ones help. but in the end it turned out to be just a naming of the button thing, stupid me;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.