如何从另一个类的构造函数中引用参数?

时间:2014-11-14 04:45:54

标签: java user-interface button constructor jframe

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);
    }

}

如何使用我在第一个类中创建的NumberFormat实例来获取要在我的Driver类(在if语句中)中的值?

另外,当我运行GUI时,我会打开两个窗口。一个是空白的,一个是我的实际程序。如何让空白的一个停止开放?

2 个答案:

答案 0 :(得分:0)

回答你的第一个问题:

在产品按钮类中,将数字格式化程序声明为私有成员,然后在构造函数中初始化。然后,为它制作一个getter方法。

在您的驱动程序类中,调用相关对象的数字格式化程序getter来格式化您的数字。

答案 1 :(得分:0)

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

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

使用与您用于产品按钮完全相同的技术。

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