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();'。對不起,第一次把你騙子。 –