GUI文本区域中的java for循环

GUI文本区域中的java for循环,java,swing,user-interface,for-loop,textarea,Java,Swing,User Interface,For Loop,Textarea,问题很简单……我有一个for循环来打印标题列表,并将结果附加到GUI的文本区域。例如,列表包含标题A、B、C。每次文本区域都应逐个显示标题。但是,在我的例子中,打印出来的总是显示整个标题列表。 我的TextArea方法在for循环中被调用。它应该附加到每个循环的打印输出,而不是整个列表。如何解决它请帮助 很抱歉造成混淆…我使用JTextArea来显示结果。代码如下: private static JTextArea textArea1; .... .... textArea1

问题很简单……我有一个for循环来打印标题列表,并将结果附加到GUI的文本区域。例如,列表包含标题A、B、C。每次文本区域都应逐个显示标题。但是,在我的例子中,打印出来的总是显示整个标题列表。 我的TextArea方法在for循环中被调用。它应该附加到每个循环的打印输出,而不是整个列表。如何解决它请帮助

很抱歉造成混淆…我使用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…很抱歉,很难解释我看到的内容


如果没有提供证据,听起来就像是在事件调度线程的上下文中循环

这意味着,在退出循环(和方法)之前,
JTextArea
的内容不会更新到屏幕上

Swing是一个单线程环境。事件调度线程负责处理绘制请求。任何阻止此线程的操作都将阻止它处理绘制更新

还需要在EDT的上下文中完成对UI的所有更新和交互

你有几个选择

您可以使用
javax.swing.Timer
,这将允许定期触发在EDT上下文中触发的
ActionEvent
。这意味着当计时器“等待”时,它不会阻止EDT

您可以使用
SwingWorker
,它允许您在后台线程中执行处理,但为您提供了
将结果发布回EDT和
处理EDT中这些更新的功能

查看更多详细信息

用示例更新

javax.swing.Timer
充当一种循环(都是不受控制的循环)。每个周期都会触发。您需要将其视为刚刚循环了另一个迭代,并根据需要更新UI的状态

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();            
        }        
    }
}


在没有证据的情况下,听起来像是在事件调度线程的上下文中循环

这意味着,在退出循环(和方法)之前,
JTextArea
的内容不会更新到屏幕上

Swing是一个单线程环境。事件调度线程负责处理绘制请求。任何阻止此线程的操作都将阻止它处理绘制更新

还需要在EDT的上下文中完成对UI的所有更新和交互

你有几个选择

您可以使用
javax.swing.Timer
,这将允许定期触发在EDT上下文中触发的
ActionEvent
。这意味着当计时器“等待”时,它不会阻止EDT

您可以使用
SwingWorker
,它允许您在后台线程中执行处理,但为您提供了
将结果发布回EDT和
处理EDT中这些更新的功能

查看更多详细信息

用示例更新

javax.swing.Timer
充当一种循环(都是不受控制的循环)。每个周期都会触发。您需要将其视为刚刚循环了另一个迭代,并根据需要更新UI的状态

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();            
        }        
    }
}


这没有任何意义。您使用的是什么GUI,哪个文本组件?你能发布一些代码或截图吗?你是说你想让他们在不同的行上,还是想看一些动画?一次一个或整个列表,这有什么区别?为你的尝试显示代码,也许有人能够提供帮助。这没有任何意义。您使用的是什么GUI,哪个文本组件?你能发布一些代码或截图吗?你是说你想让他们在单独的行上,还是想看一些动画?一次一个或整个列表,这有什么区别?显示你尝试的代码,也许有人可以帮助你。您好,我试过计时器,但不知道它是如何工作的…它似乎是执行操作的延迟…没错。Indra是,每次调用actionPerformed方法时,您都会用新内容更新文本区域,直到完成。一旦您添加了所有内容,您将停止计时器直到混淆…例如,我有:timer=new timer(5000,new ActionListener(){public void actionPerformed(ActionEvent e){for(int i=0;iNo.在每个刻度上,你只需要用下一段数据更新文本区域,直到你的数据用完。
计时器
起着一种循环的作用您好,我试过计时器,但不知道它是如何工作的……它看起来像是执行操作的延迟……没错。Indra应该是每个人都要执行操作方法时,您将使用新内容更新您的文本区域,直到完成。添加所有内容后,您将停止计时器直到混淆…例如,我有:timer=new timer(5000,new ActionListener(){public void actionPerformed(ActionEvent e){for(int i=0;iNo.在每个勾号上,您只需要用下一段数据更新文本区域,直到数据用完。
计时器是actin