使用java在GUI中显示链表的问题 [英] Problems on display linked list in GUI using java

查看:136
本文介绍了使用java在GUI中显示链表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是链表中的初学者,我正在Java GUI中创建一个带有链表的库存系统。我应该在1个节点中存储4个变量,即name,author,isbn和number。我编写了代码,但不知怎的,它没有成功添加到列表中。当我尝试显示它时,它什么都没显示。我怎么解决这个问题?



这是我的节点类:



I am a beginner in linked list and I am creating a library inventory system with linked list in Java GUI. I supposed to store 4 variables in 1 node which is name, author, isbn and number. I have wrote the code but somehow it did not adding to the list successfully. When I try to display it, it shows nothing. How can I solve this?

Here is my node class:

public class Node 
{
    String name;
    String author;
    int isbn;
    int number;
    Node next;
    
    Node()
    {
        name = null;
        author = null;
        isbn = 0;
        number = 0;
        next = null;
    }
    
    Node(String name, String author, int isbn, int number, Node next)
    {
        this.name = name;
        this.author = author;
        this.isbn = isbn;
        this.number = number;
        this.next = next;
    }
    
    String getName()
    {
        return name;
    }
    
    String getAuthor()
    {
        return author;
    }
    
    int getisbn()
    {
        return isbn;
    }
    
    int getnumber()
    {
        return number;
    }
    
    Node getNext()
    {
        return next;   
    }
    
    void setNext(Node next)
    {
        this.next=next;
    }
}





这是我的LinkedList类:





Here is my LinkedList class:

public class LinkedList
{
    Node node = new Node();
    Node head;
    
    LinkedList()
    {
        head=null;
    }
    
    Node getHead()
    {
        return head;
    }
    
    public void addNode(String name, String author, int isbn, int number)
    {
        Node newNode = new Node(name, author, isbn, number, head);
        head = newNode;
        JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory.");
    }
    
    public String displayNode()
    {
        Node current = head;
        String output = "";
        
        while(current!= null)
        {
            output+="Book Name: "+current.getName()+" Author: "+current.getAuthor()+" ISBN Number: "+current.getisbn()+" Number of copies: "+current.getnumber();
            current=current.getNext();
        }
        
        return(output+"NULL");
    }





这是我的GUI Jframe插入:





Here is my GUI Jframe for insertion:

String name;
    String author;
    int isbn;
    int number;
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        name = jTextField2.getText();
        author = jTextField3.getText();
        isbn = Integer.parseInt(jTextField4.getText());
        number = Integer.parseInt(jTextField1.getText());
        LinkedList list = new LinkedList();
        list.addNode(name, author, isbn, number);
    }  





这是我的GUI JFrame展示:





Here is my GUI JFrame for display:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        LinkedList list = new LinkedList();
        jTextArea1.setText(list.displayNode());
    } </pre>





请帮助,谢谢。



我尝试过:



我尝试过在线搜索,但没有一个解决方案可以提供帮助。

另外我还不知道我的代码有什么问题。



Please help, thank you.

What I have tried:

I have tried search online but none of the solution can help.
Also I have not know what is the problem of my code.

推荐答案

在你的 jButton1ActionPerformed 例程,您创建一个新的 LinkedList 并向其添加一个节点。然后退出该例程并清除列表。下一次你做同样的事情,所以你的所有节点和列表都被扔掉了。在您的显示例程中,您再次执行相同操作:创建新列表并尝试读取其中的节点,但新列表将为空。您应该在主类中创建一个新的 LinkedList 对象,并始终参考。



我也注意到你的两个GUI例程都被命名为 jButton1ActionPerformed 这是一个非常糟糕的主意并且必然会引起混淆。您还应该为组件指定有意义的名称而不是默认值。
In your jButton1ActionPerformed routine you create a new LinkedList and add a node to it. You then exit that routine and the list gets disposed. Next time round you do the same thing, so all your nodes and lists are being thrown away. And in your display routine you do the same again: create a new list and try to read the nodes in it, but a new list will be empty. You should create a new LinkedList object in your main class and always refer to that.

I notice also that both of your GUI routines are named jButton1ActionPerformed which is a very bad idea and bound to cause confusion. You should also give your components meaningful names rather than the default values.


这篇关于使用java在GUI中显示链表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆