JAVA - Items not being entered into the list properly?

一世执手 提交于 2019-12-20 06:19:41

问题


This is sort of a continuation from this question. I managed to figure out the whole JTextField not letting me enter anything problem. Now I have a separate problem that the item isn't being entered properly in the list.

I still think you guys should look at the GitHub repository since I'm only posting a small portion of the code here, but here's at least the relevant portions.

Here's the GUI program (or at least the relevant portions):

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

Here's the Simulator program:

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

Here's 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;
    }
...
}

Here's 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;
    }
}

I think that's everything I need to post... let me know if there's anything else!

Thanks in advance for the help!

来源:https://stackoverflow.com/questions/19140395/java-items-not-being-entered-into-the-list-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!