链表/ GUI toString()

时间:2014-04-28 19:23:06

标签: java swing linked-list

在编写一个演示链表的类的过程中,我没有得到我需要的结果。我编写了一个包含内部节点类的类,以及一个向列表添加名称和分数的插入方法,通过删除分数最低的人将列表限制为10。我还创建了一个测试GUI程序。当运行并键入insert命令时,列表不会显示任何内容,但是窗口大小会稍微改变,具体取决于我输入的命令文本字段的内容,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();
    }
}

1 个答案:

答案 0 :(得分:1)

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

总的来说,信息到达JTextArea就好了,格式错误。

首先,使用listViewrows构建columns

listView = new JTextArea(5, 20);

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

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

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

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