Java 链表/GUI toString()

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

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

链表(玩家列表)类:

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;
        }
    }
}
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 = new JTextArea(5, 20);
默认情况下,这将使
JTextArea
占据更多空间

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

第三,更改
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();
}

请总结你的问题。你从哪里得到问题?我的问题是应该显示列表的文本区域什么也不显示。例如,要在我的列表中创建一个条目,我需要键入“insert Mike 21”,然后按enter键。在此之后,“Mike 21”不会出现在文本区域中。请使用
插入21 Mike
。正在读取
String int String
您正在读取
String cmd=sc.next()->
int score=sc.nextInt()->
String name=sc.next()
sc
上交换最后两个输入值或交换最后两个调用。