2013-08-20 89 views
1

這個問題很簡單....我有一個for循環打印標題列表並將結果追加到GUI的TextArea。例如,列表包含標題A,B,C。每次TextArea應該逐個顯示標題。但是,就我而言,打印輸出總是顯示標題的整個列表。 我的TextArea方法在for循環中調用。應該爲每個循環附加打印輸出,但不是整個列表。如何解決它請幫助...java在GUI中的for循環TextArea

對不起,我感到困惑...我使用JTextArea來顯示結果。這裏是代碼:

private static JTextArea textArea1; 
.... 
.... 
    textArea1 = new JTextArea(26, 38); 
    textArea1.setLineWrap(true); 
    textArea1.setEditable(false); 
    sbrText = new JScrollPane(textArea1); 
    sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
.... 
.... 

public static void getStringText() { 

    String ptime = passdata.getTime(); 
    String ppid = passdata.getPageid(); 
    String ptitle = passdata.getTitle(); 
    String previd = passdata.getrevid(); 
    String pparentid = passdata.getParentId(); 
    String puser = passdata.getUser(); 
    String pcomments = passdata.getComments(); 

    textArea1.append("Timestamp: " + ptime + "\n" + "Pageid: " + ppid 
      + "\n" + "Title:" + ptitle + "\n" + "Revid:" + previd + "\n" 
      + "Parentid:" + pparentid + "\n" + "User:" + puser + "\n" 
      + "Comments:" + pcomments + "\n" + newline + newline); 
    textArea1.setCaretPosition(textArea1.getDocument().getLength()); 
    // System.out.println(passToText.getPageid()); 
} 

在for循環中調用getStringText()方法。

這裏是我構建的GUI。它顯示標題及其信息,不能逐一打印,在for循環中,它應該打印爲A,然後B.在GUI上顯示動畫應顯示A然後顯示B.但在我的情況下,它顯示了A,B,C同時...對不起這很難解釋,我所看到的....

here is the GUI I built

+0

這沒有任何意義。你用什麼GUI,哪個Text組件?你可以發佈一些代碼或一些截圖嗎?你是說你希望他們分開行,或者你想看到某種動畫? – Link19

+0

一次一個或全部列表,它有什麼不同? – Rakesh

+2

顯示您的嘗試代碼,並且有人可能會提供幫助。 – kiheru

回答

6

用了證據,contrie,這聽起來像你在事件調度線程的上下文中循環。

這意味着,直到退出循環(和方法),JTextArea的內容將不會被更新到屏幕

Swing是一個單線程環境。事件派發線程負責處理繪畫請求等。阻止此線程的任何內容都將阻止它處理繪畫更新。

還要求您對用戶界面的所有更新和交互,從美國東部時間範圍內完成了...

你有幾個選項...

你可以使用一個javax.swing.Timer,這將允許觸發一個ActionEvent定期基地,這是在EDT的情況下觸發。這意味着當計時器「等待」時,它不會阻止EDT ...

您可以使用SwingWorker,它允許您在後臺線程中執行處理,但爲您提供了返回結果的能力publish在EDT和process這些在美國東部時間內更新...

退房Concurrency in Swing更多細節

與例如

javax.swing.Timer AC更新ts作爲一種循環(全部是一個不受控制的循環)。每個時期都會觸發。你需要治療,就好像你剛纔循環的另一次迭代,並根據需要更新UI的狀態...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TextTimer { 

    public static void main(String[] args) { 
     new TextTimer(); 
    } 

    public TextTimer() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public static final String[] TEXT = new String[]{ 
     "Why, you wanna tell me how to live my life?", 
     "Who, are you to tell me if it's black or white?", 
     "Mama, can you hear me? Try to understand.", 
     "Is innocence the difference between a boy and a man?", 
     "My daddy lived the lie, that's just the price that he paid", 
     "Sacrificed his life, just slavin' away.", 
     "", 
     "Ohhh, if there's one thing I hang onto,", 
     "That gets me through the night.", 
     "I ain't gonna do what I don't want to,", 
     "I'm gonna live my life.", 
     "Shining like a diamond, rolling with the dice,", 
     "Standing on the ledge, I show the wind how to fly.", 
     "When the world gets in my face,", 
     "I say, Have A Nice Day.", 
     "Have A Nice Day", 
     "", 
     "Take a look around you; nothing's what it seems", 
     "We're living in the broken home of hopes and dreams,", 
     "Let me be the first to shake a helping hand.", 
     "Anybody brave enough to take a stand,", 
     "I've knocked on every door, on every dead end street,", 
     "Looking for forgiveness,", 
     "what's left to believe?", 
     "", 
     "Ohhh, if there's one thing I hang onto,", 
     "That gets me through the night.", 
     "I ain't gonna do what I don't want to,", 
     "I'm gonna live my life.", 
     "Shining like a diamond, rolling with the dice,", 
     "Standing on the ledge, I show the wind how to fly.", 
     "When the world gets in my face,", 
     "I say, Have A Nice Day.", 
     "Have A Nice Day.", 
     "", 
     "[Guitar Solo]", 
     "", 
     "Ohhh, if there's one thing I hang onto,", 
     "That gets me through the night.", 
     "I ain't gonna do what I don't want to,", 
     "I'm gonna live my life.", 
     "Shining like a diamond, rolling with the dice,", 
     "Standing on the ledge, I show the wind how to fly.", 
     "When the world gets in my face,", 
     "I say, Have A Nice Day.", 
     "Have A Nice Day.", 
     "Have A Nice Day.", 
     "Have A Nice Day.", 
     "Have A Nice Day.", 
     "", 
     "When The world keeps trying, to drag me down,", 
     "I gotta raise my hands, I'm gonna stand my ground.", 
     "Well I say, Have A Nice Day.", 
     "Have A Nice Day", 
     "Have A Nice Day" 
    }; 

    public class TestPane extends JPanel { 

     private JTextArea ta; 
     private int currentLine = 0; 

     public TestPane() { 

      setLayout(new BorderLayout()); 
      ta = new JTextArea(20, 40); 
      add(new JScrollPane(ta)); 

      Timer timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        String text = TEXT[currentLine]; 
        ta.append(text + "\n"); 
        ta.setCaretPosition(ta.getText().length()); 
        currentLine++; 
        if (currentLine >= TEXT.length) { 
         ((Timer)e.getSource()).stop(); 
        } 
       } 
      }); 
      timer.start();    
     }   
    } 
} 
+0

你好,我已經試過了定時器,但不知道它是如何工作的......它看起來像是一個延遲的行動... –

+0

是的。 Indra應該是每個被調用的actionPerformed方法,你都會用新的內容更新你的文本區域,直到你完成。一旦你添加了所有的內容,你會停止計時器 – MadProgrammer

+0

仍撲朔迷離......比如我有: 定時器=新定時器(5000,新的ActionListener(){ \t公共無效的actionPerformed(ActionEvent的五){ \t \t \t 爲\t(INT I = 0; I <10;我++){ \t \t textArea1.append第(i + 「\ n」);。 \t \t \t textArea1.setCaretPosition(textArea1.getDocument()的getLength() ); \t \t} \t} \t}); ... ... timer.start(); 它會一起追加0到9,而不是一個一個的...... 如何解決這個問題? –