0
我在我的Applet中使用一个按钮来向LinkedList添加单词。但目前它无法正常工作。似乎每次选择“添加单词”按钮时,它都会保留前一个数字作为计数值,并且以旧值递增,而不是每次都以1为单位递增。这会导致在选择搜索按钮并输出错误的编号时打印错误的值。尽管已选择“添加单词”次数,但如何让count变量保持正确的值。如何在我的Applet中阻止我的计数变量不正确递增?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* Created by joshuaogunnote on 31/10/2015.
*/
public class Applet2 extends JApplet {
private String text;
private int text1;
JTextField value1, value2;
LinkedList<String> list = new LinkedList<String>();
public JLabel jLabel;
public int count = 0;
public int search_count = 0;
public void init() {
JLabel prompt = new JLabel("Please enter a word");
JLabel prompt1 = new JLabel("Please enter a certain letter");
value1 = new JTextField(10);
value2 = new JTextField(10);
JPanel textPanel = new JPanel();
textPanel.add(prompt);
textPanel.add(value1);
add(textPanel, BorderLayout.NORTH);
textPanel.add(prompt1);
textPanel.add(value2);
JPanel centrePanel = new JPanel();
text = "";
jLabel = new JLabel(text);
centrePanel.add(jLabel);
add(centrePanel, BorderLayout.CENTER);
JButton but = new JButton("Add word");
JButton but1 = new JButton("Clear");
JButton but2 = new JButton("Remove first occurrence");
JButton but3 = new JButton("Remove all occurrences");
JButton but4 = new JButton("Display all words begging with certain letter");
JButton but5 = new JButton("Search");
JPanel butPanel = new JPanel();
butPanel.add(but);
butPanel.add(but1);
butPanel.add(but5);
butPanel.add(but2);
butPanel.add(but3);
butPanel.add(but4);
add(butPanel, BorderLayout.SOUTH);
but.addActionListener(new AddHandler(this));
but1.addActionListener(new ClearHandler(this));
but5.addActionListener(new SearchHandler(this));
but2.addActionListener(new RemoveFirstHandler(this));
}
class AddHandler implements ActionListener {
private Applet2 theApplet;
public AddHandler(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
text = theApplet.value1.getText();
try {
text1 = Integer.parseInt(text);
jLabel.setText("ERROR - The string " + "'" + text1 + "'" + " is not a valid word");
} catch (NumberFormatException e1) {
if (text.length() != 0) {
jLabel.setText("Word " + "'" + text + "'" + " has been added to the list");
list.add(text);
这是变量。
count = count + 1;
} else {
jLabel.setText("ERROR - Please enter a word");
}
}
}
}
class ClearHandler implements ActionListener {
private Applet2 theApplet;
public ClearHandler(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
list.clear();
jLabel.setText("List has been cleared");
count = 0;
search_count = 0;
}
}
class SearchHandler implements ActionListener {
private Applet2 theApplet;
public SearchHandler(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
String text = theApplet.value1.getText();
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
if (text.equals(listIterator.next())) {
search_count = search_count + 1;
}
}
jLabel.setText("Word " + "'" + text + "'" + " was found " + search_count + " time(s) in the list");
if (text.length() == 0) {
jLabel.setText("Please enter a word - The total number of words in the list are: " + count);
}
}
}
class RemoveFirstHandler implements ActionListener {
private Applet2 theApplet;
public RemoveFirstHandler(Applet2 app) {
theApplet = app;
}
public void actionPerformed(ActionEvent e) {
ListIterator<String> listIterator = list.listIterator();
while (listIterator.hasNext()) {
if (text.equals(listIterator.next())) {
list.remove(text);
jLabel.setText("First occurrence of word " + "'" + text + "'" + " has been deleted");
count = 0;
search_count = 0;
break;
} else {
jLabel.setText("ERROR - Word " + "'" + text + "'" + " is not in list");
}
}
}
}
}
此代码很荒谬。如果有什么可疑的事情发生,应该抛出异常,你需要处理。没有例外情况决定你的非流动性流程 – Stultuske
对于如何正确实施它,你有什么想法吗?我试图实现约束,使我的文本字段只接受字符串没有数字。 – jayoguntino
我不认为它会重置单击按钮上的值。你有没有调试并遵循流程? – Stultuske