I have created a static Main class called ProductButtonTestDriver, that will run my class ProductButton.java- the prof wants a picture of each coffee shop item, with the name and price to appear in a button, then a check box if the customer selects that item- I am only on the first part as of yet- I can't get a productname nor productprice to appear as a button, that is then checked off if the custoomer wants that item? I am new to Java and learning as I go. The code below does not look pretty, I have no comments, not proper spacing, try to overlook this. I have put a great deal of effort in this if that counts for something?!- I have checked a number of resources, but am getting confused- can someone help me?:0)

package productbuttontestdriver;

import java.awt.*;
import javax.swing.*;

 
public class ProductButton extends JButton {

    ImageIcon image1;
    JLabel label1, label2, label3, label4, label5, label6;
      double coffee = 3.75;
      
     
   /**
    The name of the product.
   */
   private String productName;
   
   /**
    The price for which we will sell the product
   */
   private double productPrice;

   /**
    Creates a button that will display an image of the product
    (assumed to be stored in a file starting with the specified
    name and ending with ".jpg"), the specified product name,
    and the specified price (formatted properly); the text is 
    displayed below the image and is centered.
    @param name The product name.
    @param price The selling price for this product.
   */
   public ProductButton (String name, double price) {
      /* Add code here (& only here) to complete this class. */
       price=3.75;
            name="Coffee: $" ;
            
       productPrice = price;
       productName = name;
       ImageIcon icon = new ImageIcon ("coffee.jpg");
       label1 = new JLabel ("Coffee: $" + coffee, icon, SwingConstants.CENTER);
       label1.setHorizontalTextPosition(SwingConstants.CENTER);
       label1.setVerticalTextPosition(SwingConstants.BOTTOM);
   }
    
    ProductButton() {
       
     setLayout(new FlowLayout());
    
    image1=new ImageIcon(getClass().getResource("coffee.jpg"));
    label1=new JLabel(image1);
    add(label1);  }
           
   /**
    Retrieves the product name.
    @return The product name.
   */
    @Override
   public String getName() { 
      return productName; 
   }

   /**
    Retrieves the selling price.
    @return The price for which we will sell the product.
   */
   public double getPrice() { 
      return productPrice; 
   }
}

______________________________________________________

/*
 * 
 */
package productbuttontestdriver;

import java.awt.*;
import javax.swing.*;

public class ProductButtonTestDriver {
    
        public static void main(String[] args){
            
        JFrame frame = new JFrame("Selection");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
                
        JPanel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        panel.setPreferredSize(new Dimension(200, 200));
        frame.getContentPane().add(new ProductButton());
        frame.pack();
        frame.setVisible(true);
        
        
        }
    
}

Recommended Answers

All 2 Replies

put Icon to the JButton directly

I will give that a try.

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.