对不起,我是新来的。 我目前正在使用BlueJ并浏览关于Eclipse的YouTube教程,但我需要将BlueJ用于我的作业
我只需制作一个名为GadgetShop的GUI(我已经完成),它有一些按钮可以从我的手机和MP3类中添加信息。还有一个名为Gadget的类,它是超类。
所以我遇到的问题是使用ArrayLists并从类中收集信息以在我制作的文本框中显示它。我制作了一个数组列表,但我不确定是什么错,因为它说类Gadget中的构造函数Gadget不能应用于给定的类型;
这里是GadgetShop所需的代码:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class GadgetShop implements ActionListener
{
private JTextField model, price, weight, size, credit, memory, phoneNo, duration, download, displayNumber;
private JButton addMobile, addMP3, clear, displayAll;
//These JTextField's are for the labels
private JTextField model2, price2, weight2, size2, credit2, memory2, phoneNo2, duration2, download2, displayNumber2;
private JFrame frame;
private ArrayList<Gadget> gadgetDetails;
public GadgetShop()
{
makeFrame();
}
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget Object = new Gadget();
GadgetList.add(Object.Gadget(model, price, weight, size));
}
public void addGadget(Gadget newGadget)
{
gadgetDetails = new ArrayList<Gadget>();
gadgetDetails.add(newGadget);
model.setText("s6");
我的小工具是这样的:
/**
* Gadget that is a super class for the Mobile Phone and MP3.
* Needs input for the price, weight, model and size.
*/
public class Gadget
{
// Variables
public double price;
public int weight;
public String model;
public String size;
/**
* Constructor for objects of class Gadget
*/
public Gadget(double ThePrice, int TheWeight, String TheModel, String TheSize)
{
// initialise instance variables
price = ThePrice;
weight = TheWeight;
model = TheModel;
size = TheSize;
}
public double price()
{
return price;
}
public int weight()
{
return weight;
}
public String model()
{
return model;
}
public String size()
{
return size;
}
public void print()
{
System.out.println("The price of the gadget is " + price + " pounds");
System.out.println("The weight is " + weight + " grams");
System.out.println("The model is " + weight);
System.out.println("The size is " + size);
}
}
这实际上是什么意思所以我可以解决这个问题,以及在点击按钮时从我的课程中收集信息的推荐方法是什么? (我知道如何激活点击按钮并添加信息,但只是不知道检索它的最佳方式)
感谢您的阅读,我想学习,这将对我有所帮助。
答案 0 :(得分:1)
Gadget
的构造函数接受参数:
public Gadget(double ThePrice, int TheWeight, String TheModel, String TheSize)
{
// initialise instance variables
price = ThePrice;
weight = TheWeight;
model = TheModel;
size = TheSize;
}
你不能简单地调用new Gadget()
,因为构造函数需要四个参数。您有两种选择:首先,您可以在main方法中提供参数:
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget object = new Gadget(1.00,20,"a model", "big");
GadgetList.add(object);
}
替换任何有意义的值。另一个解决方案是创建另一个不在Gadget
类中引用参数的构造函数:
public Gadget() {
//initialize values to default values
price = 1.00
weight = 21
// etc.
}
您仍需要稍微修复main
方法:
public static void main (String args[]){
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget object = new Gadget();
GadgetList.add(object);
}