JAVA - 没有正确输入列表的项目?

时间:2013-10-02 15:14:25

标签: java linked-list

这是this question的延续。我设法弄清楚整个JTextField不让我输入任何问题。现在我有一个单独的问题,即项目没有在列表中正确输入。

我仍然认为你们应该看看the GitHub repository,因为我这里只发布了一小部分代码,但这里至少是相关部分。

这是GUI计划(或至少相关部分):

import java.awt.event.*;
import javax.swing.*;

public class GUI extends JPanel implements ActionListener {
    LinkedList<FoodItem> foodItemList = new LinkedList<FoodItem>();
    ...
    protected JTextField textField;
    public GUI() {
        textField = new JTextField(20);
        textField.addActionListener(this);
        add(textField);
        ...
    }
    boolean textSubmitted = false;
    String current_command = "";
    Simulator helper = new Simulator();
    String text = "";
    double p = 0.0;
    int q = 0;
    String d = "";
    double s = 0.0;
    String so = "";
    String c = "";
    ...
    public void add_c() {
        textSubmitted = false;
        current_command = "";
        c = text;
        helper.add(foodItemList, text, p, q, d, s, so, c);
    }
    ...
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals(buttonCommands[0]))  {
            text = textField.getText();
            textSubmitted = true;
            textField.setText("");
            ...
            else if (current_command.equals("add6"))
            add_c();
            ...
        }
        ...
    }
    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Vendor Interface");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GUI newContentPane = new GUI();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }
}

这是Simulator计划:

public class Simulator {
    public void add(LinkedList<FoodItem> fi, String text, double p, int q, String d, double s, String so, String c) {
        fi.add(new FoodItem(text,p,q,d,s,so,c));
    }
    ...
}

这是LinkedList

public class LinkedList <T> implements LinkedListInterface <T> {
    public LinkedListNode <T> list;
    ...
    @Override
    public void add(T element) {
        if (!contains(element)) {
            LinkedListNode <T> newNode = new LinkedListNode<T>(element, list);
            list = newNode;
            count++;
        }
        else
            System.out.println("Element is already included.");
    }
    @Override
    public boolean contains(T element) {
        LinkedListNode<T> current = list;
        if (!isEmpty()) {
            while (current != null) {
                if (current.getElement().equals(element))
                    return true;
                current = current.getLink();
            }
        }
        return false;
    }
    public boolean containsName(String s)   {
        LinkedListNode<T> current = list;
        if (isEmpty())
            return false;
        else    {
            while (current != null) {
                if (current.getElement().getClass().getName().equals("FoodItem"))   {
                    FoodItem fi = (FoodItem) current.getElement();
                    if (fi.name.equals(s))
                        return true;
                }
            current = current.getLink();
            }
            return false;
        }
    }
    ...
    @Override
    public boolean isEmpty() {
        if (list == null)
            return true;
        else
            return false;
    }
...
}

这是LinkedListNode

public class LinkedListNode <T>{
    T element;
    LinkedListNode <T> link;
    public LinkedListNode(T element) {
        super();
        this.element = element;
        this.link = null;
    }
    public LinkedListNode(T element, LinkedListNode <T> link) {
        super();
        this.element = element;
        this.link = link;
    }
    public T getElement() {
        return element;
    }
    public void setElement(T element) {
        this.element = element;
    }
    public LinkedListNode <T> getLink() {
        return link;
    }
    public void setLink(LinkedListNode <T>link) {
        this.link = link;
    }
}

我认为这就是我需要发布的所有内容......如果还有其他内容,请告诉我们。

提前感谢您的帮助!

0 个答案:

没有答案