我有一个搜索框,用户可以输入一个州,它会从一个文本文件中读取有关该州选举结果的数据。但是我的 JTextArea 没有显示新数据。我调试并确定数据被正确读取。我已经阅读了很多与我类似的问题,但没有找到适合我的特定问题的解决方案。任何人都可以就我应该如何解决这个问题提供任何建议。这是我的代码。
package view;
import data.VoteIO;
import business.State;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
//illustrate listening for a selection of the JList
public class Voting2000 extends JFrame implements ActionListener{
private ResultsView votePanel;
private Container pane;
private JTextField search;
private JButton goSearch;
private JLabel instructions;
public Voting2000() throws IOException{
votePanel = new ResultsView(new State("Nebraska", "NE"));
search = new JTextField();
goSearch = new JButton("Search");
instructions = new JLabel("To search for a states input must be in following format State, State's abbreviate for example Nebraska, NE ");
pane = getContentPane();
goSearch.addActionListener(this);
pane.setLayout(new BorderLayout());
pane.add(BorderLayout.NORTH,instructions);
pane.add(BorderLayout.CENTER, votePanel);
pane.add(BorderLayout.SOUTH,search);
pane.add(BorderLayout.EAST,goSearch);
pack();
setVisible(true);
}
public static void main(String[] args) throws IOException{
Voting2000 listing = new Voting2000();
}
public void actionPerformed(ActionEvent e)
{
String state = search.getText().toLowerCase();
String[] fields = state.split(",");
try {
State aState = new State(fields[0].trim(),fields[1].trim());;
votePanel = new ResultsView(aState);
pane.add(BorderLayout.CENTER,votePanel);
pane.revalidate();
pane.repaint();
} catch (IOException ex) {
Logger.getLogger(Voting2000.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是创建 JTextArea 的 ResultsView 类
package view;
import javax.swing.*;
import java.util.List;
import business.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author rmildenb
*/
public class ResultsView extends JPanel{
private JTextArea results;
private Stats stat;
public ResultsView(){
createView();
}
public ResultsView(Stats state) {
this.stat = state;
createView();
}
public void createView(){
results = new JTextArea(5, 35);
JScrollPane pane = new JScrollPane(results);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(pane);
showInformation();
}
public void showInformation(){
results.setText("");
results.setText(stat.getDescription());
results.setCaretPosition(0);
}
}
我试图从容器窗格中删除 votePanel 并重新绘制它,但是当我尝试添加我刚刚创建的新的并重新绘制窗格时,什么也没有出现。