这是我得到的错误:
J:\>javac -Xlint:unchecked Files.java
Files.java:58: warning: [unchecked] unchecked call to JList(E[]) as a member of
the raw type JList
JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) {
^
where E is a type-variable:
E extends Object declared in class JList
1 warning
J:\>
这是给出错误的代码:
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Files
{
public static void main(String [] args) throws IOException
{
String filename = "";
String temp;
boolean unique = true,found = false;
ArrayList<String> passageWords = new ArrayList<String>();
String temporary;
String [] words;
ArrayList<String> dictionaryWords = new ArrayList<String>();
ArrayList<String> uniqueWords = new ArrayList<String>();
filename = JOptionPane.showInputDialog(null,"Enter the name of the file you would like to display a unique list of words for","Filename input",1);
File Passage = new File(filename);
Scanner in = new Scanner(Passage);
while(in.hasNext())
{
temp = in.nextLine();
temp = temp.toLowerCase();
temp = temp.replace("\\s+"," ");
temp = temp.replace(".","");
temp = temp.replace("\"","");
temp = temp.replace("!","");
temp = temp.replace("?","");
temp = temp.replace(",","");
temp = temp.replace(";","");
temp = temp.replace(":","");
temp = temp.replace("/","");
temp = temp.replace("\\","");
temp = temp.trim();
words = temp.split(" ");
for(int c = 0;c <words.length;c++)
passageWords.add(words[c]);
}
File dictionary = new File("wordList.txt");
Scanner input = new Scanner(dictionary);
while(input.hasNext())
{
temporary = input.nextLine();
dictionaryWords.add(temporary);
}
for(int counter = 0;counter<passageWords.size();counter++)
{
unique = true;
for(int count = 0;count<dictionaryWords.size();count++)
{
if((passageWords.get(counter).contentEquals(dictionaryWords.get(count))))
unique = false;
}
if(unique)
uniqueWords.add(passageWords.get(counter));
}
JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 250);
}
};;
JOptionPane.showMessageDialog(null,pane,"Unique Words",1);
for(int counts = 0;counts<uniqueWords.size();counts++)
{
for(int counters = 1;counters<uniqueWords.size();counters++)
{
if((uniqueWords.get(counts)).contentEquals(uniqueWords.get(counters)))
{
uniqueWords.remove(counters);
counters--;
}
}
}
JOptionPane.showMessageDialog(null,pane,"Unique Words",1);
}
}
目前的代码是读取两个文件,其中一个代表字典,另一个代表文本。它旨在检查词典中没有哪些单词并将其打印出来。代码中还有一些其他错误,但我只想先对它进行排序。
答案 0 :(得分:0)
JList是一种通用类型,您可以将其用作原始类型。
使用new JList<Object>(uniqueWords.toArray())
,或者更好的是,如果您想要JList<String>
:
String[] wordsAsArray = uniqueWords.toArray(new String[uniqueWords.size()]);
JScrollPane pane = new JScrollPane(new JList<String>(wordsAsArray));
请注意,您的问题比代码中的问题更严重。第一个是不正确的缩进,这使得它不可读。第二个是您使用主线程中的Swing组件,尽管它的文档非常清楚,它们只能从事件派发线程中访问。