[英]linked list/GUI toString()
在編寫演示鏈接列表的類的過程中,我沒有得到所需的結果。 我編寫了一個包含內部節點類的類,以及一個將名稱和分數添加到列表中的插入方法,通過刪除分數最低的人將列表限制為十個。 我還創建了一個測試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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.