import java.awt.*;
import javax.swing.*;
import java.text.NumberFormat;
public class ProductButton extends JButton {
private String productName;
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) {
productName = name;
productPrice = price;
ImageIcon icon = new ImageIcon(name + ".jpg");
this.setIcon(icon);
NumberFormat f = NumberFormat.getCurrencyInstance();
this.setText(f.format(price));
this.setHorizontalTextPosition(JButton.CENTER);
this.setVerticalTextPosition(JButton.BOTTOM);
}
public String getName() {
return productName;
}
public double getPrice() {
return productPrice;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnackShopFrame extends JFrame implements ActionListener{
private ProductButton coffeeButton, cocoaButton, donutButton, cookieButton, muffinButton, cupcakeButton;
private JButton newCustomer;
private JLabel totalPrintOut, totalPriceString;
private double totalPrice;
public SnackShopFrame(){
JFrame window = new JFrame("Snack Shop Register");
window.setSize(500,700);
JPanel first = new JPanel();
JLabel direction = new JLabel("Click on the products that the customer wishes to purchase:");
first.add(direction);
first.setLayout(new BorderLayout());
first.add(direction, BorderLayout.NORTH);
JPanel second = new JPanel();
second.setLayout(new GridLayout(3,2,10,10));
coffeeButton = new ProductButton("coffee", 3.75);
coffeeButton.addActionListener(this);
second.add(coffeeButton);
cocoaButton = new ProductButton("cocoa", 2.25);
cocoaButton.addActionListener(this);
second.add(cocoaButton);
donutButton = new ProductButton("donut",1.50);
donutButton.addActionListener(this);
second.add(donutButton);
cookieButton = new ProductButton("cookie", 1.25);
cookieButton.addActionListener(this);
second.add(cookieButton);
muffinButton = new ProductButton("muffin", 1.75);
muffinButton.addActionListener(this);
second.add(muffinButton);
cupcakeButton = new ProductButton("cupcake", 1.50);
cupcakeButton.addActionListener(this);
second.add(cupcakeButton);
JPanel third = new JPanel();
totalPrintOut = new JLabel("");
third.add(totalPrintOut);
newCustomer = new JButton("Next Customer");
newCustomer.addActionListener(this);
third.add(newCustomer);
Container contentPane = window.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(first, BorderLayout.NORTH);
contentPane.add(second, BorderLayout.CENTER);
contentPane.add(third, BorderLayout.SOUTH);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() != newCustomer){
if(e.getSource() == coffeeButton){
totalPrice += 3.75;
}
if(e.getSource() == cocoaButton){
totalPrice += 2.25;
}
if (e.getSource() == donutButton){
totalPrice += 1.50;
}
if (e.getSource() == cookieButton){
totalPrice += 1.25;
}
if (e.getSource() == muffinButton){
totalPrice += 1.75;
}
if (e.getSource() == cupcakeButton){
totalPrice += 1.50;
}
}
if(e.getSource() == newCustomer){
totalPrice = 0;
}
totalPriceString = new JLabel(String.valueOf(totalPrice));
totalPrintOut.setText("Current total: $" + totalPrice);
}
public static void main (String[] args){
new SnackShopFrame().setVisible(true);
}
}
我如何得到我的驱动程序类中的值(在if语句)使用我在头等舱做出的NumberFormat实例进行格式化?
此外,当我运行GUI,我得到两个窗口打开。一个是空白的,一个是我的实际程序。我如何得到空白的一个停止开放?
我知道这不是什么你问 - 但不是所有的'if'语句,你可以只写'totalPrice + = e.getSource()用getPrice();' –
这将帮助我如果我可以参考价格,很多。它在我的构造函数中,并且我不知道如何在我的驱动程序类中引用该参数。你知道我该怎么做吗? –
哈,对不起,我在最后的评论中犯了一个错误。我相信它应该是'totalPrice + =((ProductButton)e.getSource())。getPrice();'。对不起,第一次把你骗子。 –