由於某些原因,我無法獲取JTextArea「answersTA」來顯示任何內容。調用append()和setText()不會更新框中的信息。JTextArea沒有顯示我告訴它的內容
這是我的程序的目標:取一個字符串並將其存儲到「字」中,接受一個字符串並從中解析一個int並將其存儲爲「num」,將這些信息輸入到solution()方法中,並顯示由solutions()返回的數組。我不能在生活中得到任何東西來展示。
public class CWGui extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 400;
private JLabel pattern, number, answers;
private JTextField patternTF, numberTF;
private JButton execute, exitB;
private JTextArea answersTA;
//Button handlers:
private ExecuteButtonHandler eHandler;
private ExitButtonHandler ebHandler;
public CWGui()
{
pattern = new JLabel("Enter the pattern: ", SwingConstants.RIGHT);
number = new JLabel("Enter the number of solutions: ", SwingConstants.RIGHT);
answers = new JLabel("Solutions: ", SwingConstants.LEFT);
patternTF = new JTextField(10);
numberTF = new JTextField(10);
answersTA = new JTextArea();
//SPecify handlers for each button and add (register) ActionListeners to each button.
execute = new JButton("Execute");
eHandler = new ExecuteButtonHandler();
execute.addActionListener(ebHandler);
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
setTitle("Crossword Solution Generator");
Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 2));
//Add things to the pane in the order you want them to appear (left to right, top to bottom)
pane.add(pattern);
pane.add(patternTF);
pane.add(number);
pane.add(numberTF);
pane.add(execute);
pane.add(exitB);
pane.add(answers);
pane.add(answersTA);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class ExecuteButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
String word = patternTF.getText();
int num = Integer.parseInt(numberTF.getText());
FileParser fp = new FileParser("TWL06.txt");
List<String> dict = fp.getAllWords();
CWSolution c = new CWSolution(dict);
List<String> result = c.solutions(word,num);
answersTA.setText(result.toString());
}
catch(Exception t){}
}
}
public class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
CWGui generator = new CWGui();
}
}
看起來更像你的'CWSolution'正在從'解決方案(單詞,數字)'中返回一些什麼都沒有。考慮發佈該代碼。也永遠不要默默地捕捉所有例外。因爲你在做什麼都可能是問題。找出是否拋出異常。 – Radiodef