不好意思,我是新来的。
我目前正在使用BlueJ,并正在Eclipse上浏览YouTube教程,但我需要使用BlueJ进行作业

我只需要制作一个名为GadgetShop的GUI(已完成),它就有一些按钮,可以添加来自Mobile和MP3类的信息。还有一个称为小工具的类,它是超类。

所以我遇到的问题是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);
}
}


它实际上是什么意思,所以我可以解决此问题,以及在单击按钮时从班级收集信息以输入到文本框中的推荐方法是什么? (我知道如何激活点击按钮并添加信息,但只是不知道检索它的最佳方法)

感谢您的阅读,我想学习,这样对我有很大帮助。

最佳答案

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

07-28 02:38