2014-11-14 57 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的構造函數。我們不允許改變任何東西在代碼中。 –

相關問題