java - 链表/GUI toString()

标签 java swing linked-list

在编写一个演示链表的类的过程中,我没有得到我需要的结果。我编写了一个类,其中包含一个内部节点类以及一个插入方法,该方法将名称和分数添加到列表中,通过删除分数最低的人将列表限制为 10 个。我还创建了一个测试 GUI 程序。当运行并键入插入命令时,列表不会显示任何内容,但是窗口大小会根据我在命令文本字段中键入的内容而略有变化,这表明 pack() 方法可以“查看”我正在编写的内容。我怀疑我的问题出在位于 GamerList 类中的 toString() 方法中的某个地方。

链接列表 (GamerList) 类:

class GameList
{
    //node class

    private class Node
    {
        String name;
        int score;
        Node next;

        //Node constructor
        Node(String namVal, int scrVal, Node n)
        {
            name = namVal;
            score = scrVal;
            next = n;
        }

        //Constructor
        Node(String namVal, int scrVal)
        {
            this(namVal, scrVal, null);
        }
    }

    private Node first; //head
    private Node last;  //last element in list

    //Constructor

    public GameList()
    {
        first = null;
        last = null;
    }

    //isEmpty method: checks if param first is empty
    public boolean isEmpty()
    {
        return first == null;
    }

    public int size()
    {
        int count = 0;
        Node p = first;

        while(p != null)
        {
            count++;
            p = p.next;
        }
        return count;
    }

  public String toString()
{
  StringBuilder strBuilder = new StringBuilder();

  Node p = first;
  Node r = first;
  while (p != null)
  {
     strBuilder.append(p.name + " ");
     p = p.next;
  }
      while (r != null)
  {
     strBuilder.append(r.score + "\n");
     r = r.next;
  }      
  return strBuilder.toString(); 
}

    public void insert(String name, int score)
    {
        Node node = new Node(name, score);
        final int MAX_LIST_LEN = 10;

        if(isEmpty())
        {
            first = node;
            first.next = last;
        }

        else if(first.score <= node.score)
        {
            node.next = first;
            first = node;
        }

        else
        {
            Node frontNode = first;
            while(frontNode.score > node.score && frontNode.next != null)
            {
                frontNode = frontNode.next;
            }
            node.next = frontNode.next;
            frontNode.next = node;
        }

        if(size() > MAX_LIST_LEN)
        {
            Node player = first;
            for(int i = 0; i < 9; i++)
            {
                player = player.next;
            }
            player.next = null;
        }
    }
}

GUI测试程序:

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

/**
 This class is used to demonstrate
 the operations in the GameList class.
 */

public class GameListGui extends JFrame
{
    private  GameList topGamers;
    private JTextArea  listView;
    private JTextField cmdTextField;

    public GameListGui()
    {
        topGamers = new GameList();
        listView = new JTextArea();
        cmdTextField = new JTextField();

        // Create a panel and label for result field
        JPanel resultPanel = new JPanel(new GridLayout(1,2));
        resultPanel.add(new JLabel("Command Result"));
        add(resultPanel, BorderLayout.NORTH);

        // Put the textArea in the center of the frame
        add(listView);
        listView.setEditable(false);
        listView.setBackground(Color.WHITE);

        // Create a panel and label for the command text field
        JPanel cmdPanel = new JPanel(new GridLayout(1,2));
        cmdPanel.add(new JLabel("Command:"));
        cmdPanel.add(cmdTextField);
        add(cmdPanel, BorderLayout.SOUTH);
        cmdTextField.addActionListener(new CmdTextListener());

        // Set up the frame
        setTitle("Linked List Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private class CmdTextListener
            implements ActionListener
    {
        public void actionPerformed(ActionEvent evt)
        {
            String cmdText = cmdTextField.getText();
            Scanner sc = new Scanner(cmdText);
            String cmd = sc.next();
            if (cmd.equals("insert")){
            if (sc.hasNext())
            {
                // add index element
                String name = sc.next();
                int score = sc.nextInt();

                topGamers.insert(name, score);                
            }
            listView.setText(topGamers.toString());
            pack();
            return;
            }
        }
    }
    public static void main(String [ ] args)
    {
        new GameListGui();
    }
}

最佳答案

你的界面有点笨拙。您可以使用 JTextField 作为名称,使用 JSpinner 作为分数,并使用 JButton 将值插入到链接列表中,但这只是我的想法。 ..

总体而言,信息到达 JTextArea 一切正常,但格式错误。

首先,用构造listView

listView = new JTextArea(5, 20);

这将使JTextArea默认占用更多空间。

其次,将JTextArea放在JScrollPane中,这将允许内容自动滚动超出窗口的可视大小,这意味着您可以摆脱ActionListener 中的 pack

第三,更改GameList中的toString方法。必须分离循环似乎没有意义,相反,在单个循环的每次迭代中包含您需要的所有信息,例如......

public String toString() {
    StringBuilder strBuilder = new StringBuilder();

    Node p = first;
    //Node r = first;
    while (p != null) {
        strBuilder.append(p.name).append(" ");
        strBuilder.append(p.score).append("\n");
        p = p.next;
    }
    //while (r != null) {
    //    strBuilder.append(r.score).append("\n");
    //    r = r.next;
    //}
    return strBuilder.toString();
}

关于java - 链表/GUI toString(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23349506/

相关文章:

java - 如何使用 Intellij 和 swing 界面选项卡创建类似功能?

java - 从 JPanel 中删除 JPanel - 仅适用于其自己的类

c++ - 理解为什么存储在链表中的字符串不能正确打印

C "Error: Expecter expression before ' 列表'"

java - 具有一对多关系的 HQL 给出 "ORA-00936: missing expression"异常

java - 尝试将 rtf 文件从 Jar 读取到 JEditorPane;在 Netbeans 中工作,而不是在 Jar 中工作

java - p :commandLink action does not fire

c++ - 如何指向链表中的下一个内存?

java - 使用 S3 Java SDK 与 S3 兼容存储(minio)对话

java - 如何将文本文件中的特定行获取到数组列表中?