在编写一个演示链表的类的过程中,我没有得到所需的结果。我写了一个类,它包含一个内部节点类和一个插入方法,该方法将名称和分数添加到列表中,并通过删除分数最低的人将列表限制为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();
}
}
您的界面有点笨重。你可以使用JTextField
作为名称,JSpinner
作为分数,JButton
将值插入链表,但这只是我…
总的来说,信息到达JTextArea
很好,格式错误。
首先,用rows
和columns
构建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();
}