构造函数,getter和setter不适用于JApplet

时间:2013-11-24 03:47:58

标签: java constructor setter getter japplet

我从未真正使用过getter,setter和构造函数,但我必须使用它们,因为我需要在JApplet上绘制很多变量。

然而,我放入新构造函数的值都没有像我想要的那样被绘制到applet上。相反,在paint方法中调用新对象也没有用。 (当我自己输入值时,它会起作用,但这很多都是复制和粘贴。)

有没有办法可以使用我的构造函数将图像和字符串绘制到JApplet上?如果我表现不可能,我宁愿早点告诉我,所以我可以尝试其他的东西。

String name;
String description;
double price = 0;
int quantity = 0;
BufferedImage image;
public Product(String nm, String desc, double pri, int quant, String barc, String prod)
{
    y = y+155;
    name = nm;
    description = desc;
    price = pri;
    quantity = quant;
    barcode = barc;
    product = prod;
}
public String getName()
{
     return name;
}
public String getDescription()
{
     return description;
}
public double getPrice()
{
     return price;
}
public BufferedImage getImage()
{
    return image;
}
public void paint(Graphics g)
{
    super.paint(g);
    g.drawString("Welcome to Amber's Tea Party!", 15, 70);
    g.drawString("Check out our premium teas!", 15, 95);
    g.drawString(getName(), 15, y);
    g.drawString(getDescription(), 200, y);
    g.drawImage(getImage(), 15, y+15, null);
}
public static void main(String[] args)
{
    Product greenTea = new Product("Green Tea", "A tea made from herbal leaves", 1.5, 100, "/home/kenzo/Documents/CSC 130/JavaApp/src/greentea.jpeg","/home/kenzo/Documents/CSC 130/JavaApp/src/greentea.jpeg");
    greenTea.paint(null);
    Product barley = new Product("Barley Tea", "A tea made from crushed barley beans", 1.5, 100, "/home/kenzo/Documents/CSC 130/JavaApp/src/barleytea.jpeg","/home/kenzo/Documents/CSC 130/JavaApp/src/barleytea.jpeg");
    barley.paint(null);
}

0 个答案:

没有答案