JTextArea没有显示我告诉它的内容

时间:2013-12-07 19:16:40

标签: java swing jframe jtextarea

出于某种原因,我无法获得JTextArea“answersTA”来显示任何内容。对append()和setText()的调用不会更新框中的信息。

这是我的程序的目标:接受一个字符串并将其存储到“word”,接受一个字符串并从中解析一个int并将其存储到“num”,将它们提供给solutions()方法,并且显示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();
    }

}

2 个答案:

答案 0 :(得分:1)

确保对Event Dispatch Thread调用的gui进行任何更新。这个线程将更新gui,因此它不会冻结它,也正因为如此,确保任何可能需要一段时间的进程在SwingWorker线程上运行,该线程与EDT分开。 SwingWorker线程可以在后台运行,同时还可以在此过程中为您提供更新。您可以在官方文档上对这些进行详细描述。我刚刚学会了它并且它帮助了很多。

答案 1 :(得分:0)

你怎么知道没有返回例外?。

catch(Exception t){}

该行 DANGEROUS

更改此

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)的内容是什么?