0
我有一個搜索框,用戶可以鍵入一個狀態,它將從關於該州選舉結果的文本文件讀取數據。但是我的JTextArea不顯示新的數據。我進行了調試,並確定數據正在正確讀取。我讀過很多類似於我的問題,但沒有找到解決方案來解決我的特定問題。任何人都可以提供任何建議,我應該如何去做這件事。這是我的代碼。使用從文件中讀入的新數據更新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);
}
}
}
這裏是
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它其運作重繪JTextArea是創建ResultsView類,但是當我嘗試添加新我剛剛創建並重新繪製窗格沒有出現。
是的,我把代碼放在了我將它重新添加到容器中嘗試不同的事情。除了revalidate()之外,我有所有這些。我希望這是失蹤的關鍵。現在去測試。謝謝。 – dsquaredtech