在WordPair()类中,方法remove()应该接收一个数组并拆分其中的字符串,然后将这些字符串添加到两个不同的数组列表中。 wordListB可以正常工作,并且数组可以很好地填充。同样,当我在此方法内部进行系统打印时,它也会正确打印出wordListC。当在JlistFromFile类的wordSelection方法中调用getWord方法以从worldListC的特定索引中检索字符串时,遇到了我遇到的问题,此时数组打印为空。我在主体中设置了一个测试,该类本身正在按预期工作。我不知道是什么使arrayListC被清除。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Collections;
import java.util.Arrays;
import java.io.*;
/* Simple example of using the contents of a file to populate a JList
*
* @author Jill Courte
*/
public class JListFromFile extends JFrame {
private TextField wordA;
private TextField wordB;
private JButton openButton;
private JButton newButton;
private JButton addButton;
private JButton deleteButton;
private JButton saveButton;
private TextField output;
private JList listFromFile;
private JPanel listPanel;
private JPanel textPanel;
private JPanel inputPanel;
private JPanel buttonsPanel;
private SimpleWordList dataSource;
private WordPair wordPair;
public JListFromFile ()
{
// create the object to provide the data
dataSource = new SimpleWordList();
wordPair = new WordPair();
// use a border layout for the main window
getContentPane().setLayout(new BorderLayout(20, 20));
buttonsPanel = new JPanel();
openButton = new JButton("Open");
newButton = new JButton("New");
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
saveButton = new JButton("Save");
addButton.setEnabled( false );
deleteButton.setEnabled( false );
saveButton.setEnabled( false );
buttonsPanel.add(openButton);
buttonsPanel.add(newButton);
buttonsPanel.add(addButton);
buttonsPanel.add(deleteButton);
buttonsPanel.add(saveButton);
//add the button listeners
openButton.addActionListener(new OpenButtonListener());
addButton.addActionListener(new AddButtonListener());
add(buttonsPanel, BorderLayout.NORTH);
// create the panel to hold the list
listPanel = new JPanel();
// create your JList
listFromFile = new JList();
listFromFile.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFromFile.setSelectedIndex(0);
//add the list selection listener
listFromFile.addListSelectionListener(new WordSelection());
//add the translation text box
textPanel = new JPanel();
output = new TextField();
output.setEditable(false);
output.setPreferredSize(new Dimension(200, 25));
textPanel.add(output);
// add scroll bars to list
JScrollPane listScrollPane = new JScrollPane(listFromFile);
listScrollPane.setPreferredSize(new Dimension(150, 200));
//add user input textfield
wordA = new TextField ("Word to Add");
wordB = new TextField ("Translated word");
wordA.setPreferredSize(new Dimension(200, 25));
wordB.setPreferredSize(new Dimension(200, 25));
wordA.setEnabled(false);
wordB.setEnabled(false);
//create panel to hold input textfield
inputPanel = new JPanel();
inputPanel.add(wordA);
inputPanel.add(wordB);
//add the list (via the scroll pane) to the panel
listPanel.add(listScrollPane);
//add the panels to the frame
add(listPanel, BorderLayout.WEST);
add(textPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
pack();
}
private File findFile ()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Start in current directory
chooser.setCurrentDirectory(new File("."));
int status = chooser.showOpenDialog(null);
if (status != JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "No File Selected");
throw new NoFileChoosenException();
}
else
{
File file = chooser.getSelectedFile();
System.out.println(file.getName());
System.out.println(file.getPath());
return file;
}
}
private class OpenButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
listFromFile.setListData(new Object[0]);
File file = null;
try
{
file = findFile();
//tell the data source what file to use
dataSource.createList(file);
//JList needs an array of strings, retrieve it from the data source
String [] data = dataSource.getData();
//data = dataSource.insertion(data);
//data = wordPair.remove(data);
if (data != null)
listFromFile.setListData(data);
}
catch (Exception e)
{
}
addButton.setEnabled(true);
deleteButton.setEnabled(true);
saveButton.setEnabled(true);
wordA.setEnabled(false);
wordB.setEnabled(false);
}
}
private class AddButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
wordA.setEnabled(true);
wordB.setEnabled(true);
String inputStringA = wordA.getText();
String inputStringB = wordB.getText();
//dataSource.addToList(inputStringA, inputStringB);
String [] data2 = dataSource.getData();
//data2 = dataSource.insertion(data2);
// data2 = wordPair.remove(data2);
listFromFile.setListData(new Object[0]);
listFromFile.setListData(data2);
}
}
private class WordSelection implements ListSelectionListener
{
public void valueChanged (ListSelectionEvent event)
{
int index = listFromFile.getSelectedIndex();
if (index >= 0)
{
String s = wordPair.getWord(index);
output.setText(s);
}
}
}
public static void main(String[] args)
{
JListFromFile jListFromFile = new JListFromFile();
jListFromFile.setVisible(true);
}
}
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.ListIterator;
/*example of using collection class and input parsing for CIT 271
*@author Jill Courte
*/
public class SimpleWordList {
private ArrayList<String> wordListA = new ArrayList<String>();
private ArrayList<String> wordListB = new ArrayList<String>();
private ArrayList<String> wordListC = new ArrayList<String>();
private File currentFile = null;
private WordPair wordPair;
//words are in the file one word on a line
//words are read, checked for duplicates, and inserted into the file
public void createList (File file)
{
Scanner fileIn = null;
currentFile = file;
String line;
try
{
fileIn = new Scanner(new FileInputStream(currentFile.getPath()));
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
while (fileIn.hasNextLine())
{
line = fileIn.nextLine();
if (! isDuplicate(line))
insertIntoList(line);
}
fileIn.close();
}
//Uses an iterator to check for the existance of the given
//word in the list
private boolean isDuplicate (String word)
{
Iterator<String> it = wordListA.iterator();
while (it.hasNext())
{
//not ignoring case to look in class at some issues with input
String s = it.next();
if (word.equals(s))
return true;
}
return false;
}
private void insertIntoList (String word)
{
int index = 0;
boolean positionFound = false;
int result;
while (! positionFound && index < wordListA.size())
{
String tmp = wordListA.get(index);
result = tmp.compareTo(word);
if (result > 0)
positionFound = true;
else
index++;
}
//index holds the position to insert the word
wordListA.add(index, word);
//System.out.println(word);
}
public String [] getData ()
{
String [] arr = new String[wordListA.size()];
wordPair = new WordPair();
String [] temp2 = wordPair.remove(wordListA.toArray(arr));
// use the collection class method to turn this into a
// primitive array
return temp2;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
public class WordPair
{
private ArrayList<String> wordListB;
private ArrayList<String> wordListC;
private String [] arrayA;
private String [] arrayB;
private String wordA;
private String wordB;
public String[] remove (String numList[])
{
wordListB = new ArrayList<String>();
wordListC = new ArrayList<String>();
ArrayList<String> ar = new ArrayList<String>();
for( int i=0; i < numList.length; i++)
{
String string = numList[i];
String[] parts = string.split("=");
wordA = parts[0];
wordListB.add(wordA);
wordB = parts[1];
wordListC.add(wordB);
}
System.out.println(wordListC);
arrayA = wordListB.toArray(new String[wordListB.size()]);
return arrayA;
}
public String getWord(int index)
{
System.out.println(wordListC);
return wordListC.get(index);
}
}
最佳答案
在您的JListFromFile类中,您创建一个WordPair字段wordPair,但是您从不提供我可以看到的任何数据。因此,对我来说这将是空的。
是的,您在调用getData()
时提供了一个WordPair对象数据,但它与JListFromFile对象所持有的WordPair对象不同。而是由SimpleWordList变量dataSource保留的WordPair对象。
也许您希望两个对象共享同一个WordPair对象,如果是这样,则只需编写一些代码即可:
wordPair = new WordPair();
dataSource = new SimpleWordList(wordPair); // note the parameter
别处
class SimpleWordList {
private ArrayList<String> wordListA = new ArrayList<String>();
private ArrayList<String> wordListB = new ArrayList<String>();
private ArrayList<String> wordListC = new ArrayList<String>();
private File currentFile = null;
private WordPair wordPair;
// note the assignment constructor
public SimpleWordList(WordPair wordPair) {
this.wordPair = wordPair;
}
和其他地方:
public String[] getData() {
String[] arr = new String[wordListA.size()];
// ****** don't create a new object -- use the one we have ***
// wordPair = new WordPair(); ****** don't create a new one!
String[] temp2 = wordPair.remove(wordListA.toArray(arr));
// use the collection class method to turn this into a
// primitive array
return temp2;
}