Java程序JButton在行动后冻结程序,需要多次按下

时间:2016-04-11 23:19:15

标签: java swing concurrency jbutton

我正在设计一个程序,接受用户的股票行情符号和股票数量,我希望它将信息添加到ArrayList中,一旦完成这些程序,我们就会将输入打印到列表文本区域。我对我如何编写代码有一个大概的想法,但每当我按下按钮时,程序就会冻结。还有,有办法让它打印出arraylist中的所有内容吗?我把它设置为for循环,它只是将它设置为最后输入的字符串。

private void GoButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                          


    String ts1 = tickerSymbol1.getText();

    int s1 = Integer.parseInt(shares1.getText());

    String site = "http://finance.yahoo.com/d/quotes.csv?s=%22%20"+ts1+"%20+%20%22&f=sl1d1t1c1ohgv&e=.csv";
    try {
        URL stockURL = new URL(site);
        URLConnection data = stockURL.openConnection();
        Scanner input = new Scanner(data.getInputStream());

        if (input.hasNext())

        while (input.hasNextLine())
        {
            String line = input.nextLine();
            String[] stockinfo = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
            total1.setText(stockinfo[1]);


        }
    } catch (MalformedURLException ex) {
        JOptionPane.showMessageDialog(null, ex);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, ex);
    } 

}                                                                             

private void AddActionPerformed(java.awt.event.ActionEvent evt) {                                    
    ArrayList<String> stocks = new ArrayList<String>();
    String input = tickerSymbol1.getText();
    do { 
        stocks.add(input);
    }while (input.equalsIgnoreCase("quit"));


            for (int i=0;i<stocks.size();i++)
            {
                jTextArea1.setText(stocks.get(i));
            }    
}                                   

0 个答案:

没有答案