2013-12-07 36 views
0

由於某些原因,我無法獲取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(); 
    } 

} 
+0

看起來更像你的'CWSolution'正在從'解決方案(單詞,數字)'中返回一些什麼都沒有。考慮發佈該代碼。也永遠不要默默地捕捉所有例外。因爲你在做什麼都可能是問題。找出是否拋出異常。 – Radiodef

回答

0

你怎麼知道沒有返回異常?

catch(Exception t){} 

這條線是危險

更改此

catch(Exception t){} 

到這樣的事情(捕捉一般例外是不是一個好主意)

catch(Exception t){ 
t.printStackTrace();// this only for debug purpose 
JOptionPane.showMessageDialog(null,t.getMessage()); 
} 

如果它不添加類來編譯SSCCE

List<String> result = c.solutions(word,num); 
String[] array = new String [result.size()]; 
result.toArray(array); 
answersTA.setText(array.toString()); 

其實這不是必要的。

List<String> result = c.solutions(word,num); 
answersTA.setText(result.toString()); 

什麼返回c.solutions(word,num)您是否嘗試調試它?

+0

好的,我編輯了該行。對圖形用戶界面輸出沒有影響,雖然:( – iNfluence

+0

@ user3078311考慮增加類能夠讓我們測試你的代碼!造成這樣,它是不可能的,你嘗試調試運行呢? – nachokk

+0

有跡象表明,CWSolution和FileParser背後運行5班。是有什麼辦法可以附加文件?我不想在5類價值的代碼粘貼哈哈 – iNfluence

1

確保你做任何更新由事件指派線程調用​​GUI。此線程將更新gui,因此它不會凍結它,因此,請確保可能需要一段時間的任何進程都在SwingWorker線程上運行,該線程與EDT分開。 SwingWorker線程可以在後臺運行,同時還可以在此過程中爲您提供更新。您可以在官方文檔中對這些進行很好的描述。我最近才瞭解到這一點,並且幫助了很多。