2014-11-14 33 views
-1
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,我得到两个窗口打开。一个是空白的,一个是我的实际程序。我如何得到空白的一个停止开放?

+0

我知道这不是什么你问 - 但不是所有的'if'语句,你可以只写'totalPrice + = e.getSource()用getPrice();' –

+0

这将帮助我如果我可以参考价格,很多。它在我的构造函数中,并且我不知道如何在我的驱动程序类中引用该参数。你知道我该怎么做吗? –

+0

哈,对不起,我在最后的评论中犯了一个错误。我相信它应该是'totalPrice + =((ProductButton)e.getSource())。getPrice();'。对不起,第一次把你骗子。 –

回答

0

这里是做这件事的一种方式。绝对不是唯一的方式或最有效率的,但可能是最容易理解的。而不是写作

totalPriceString = new JLabel(String.valueOf(totalPrice)); 
totalPrintOut.setText("Current total: $" + totalPrice); 

只是使用完全相同的技术,你用于产品按钮。 。

NumberFormat f = NumberFormat.getCurrencyInstance(); 
totalPrintOut.setText("Current total: " + f.format(totalPrice)); 
+0

这肯定工作,但有人告诉我要利用我在ProductButton类所作的NumberFormat实例的。你能帮我吗? –

+0

但是你在做六个不同的'NumberFormat'情况下,你的' ProductButton'类,如果你只需要一个,你就必须在某个地方使用一个'static'变量。 –

0

要回答你的第一个问题:

在你的产品的按钮类,申报数量为格式化的私有成员,然后在构造函数初始化。然后,为它做一个getter方法。

在你的驱动程序类,调用该对象的你的电话号码格式的getter问题来格式化你的电话号码。

+0

我的教授告诉我们没有这样做,她要我们做这一切在ProductButton.java的构造函数。我们不允许改变任何东西在代码中。 –

相关问题