Java TextArea即时更新

时间:2013-11-09 15:14:59

标签: java swing jtextarea thread-sleep

我正在编写一个程序,我遇到了在TextArea上即时打印结果的问题(类似于你可以使用System.out.print(“”))并且需要将它们附加到文字区域。我想用它作为流程操纵窗口。

我试图调用新线程和其他一些东西,但没有成功。我真的被困在这里。

任何帮助都会非常感激。

以下是示例代码。在StdOut中,您可以看到代码执行的时间,此时它也应该在TextArea中可见,但在方法完成时它会显示在它上面。它应该在执行stdout的同时在TextArea上附加文本。


public class Start {
public static void main(String[] args) {
    Manager.getInstance();
}
}

public class Manager {

public static Manager instance;
private MyFrame mainFrame;

private Manager(){
    mainFrame = MyFrame.newInstance();
    mainFrame.setVisible(true);
}

public static Manager getInstance() {
    if(instance == null){
        instance = new Manager();
    }
    return instance;
}

public void startMethod() {
    for(int i = 0; i < 5000; i++){
        doSmething();
        if(i == 0){
            sendMsg("Start");
        }

    }
    sendMsg("End");

}

private void doSmething() {
    try{
        Thread.sleep(1);
    }
    catch (InterruptedException e){
    }
}

private void sendMsg(String str){
    (new Thread(new MyFrameMsg(mainFrame,str))).start();
    mainFrame.setTextArea(str);
    System.out.println(str);
}

}

class MyFrameMsg implements Runnable{

private MyFrame mainFrame;
private String str;

public MyFrameMsg(MyFrame _mainComander, String _str) {
    mainFrame = _mainComander;
    str = _str;
}

@Override
public void run() {
    System.out.println(str + " in thread");
    mainFrame.setTextArea(str);
}
}

<小时/> 使用Netbeans构建

public class MyFrame extends javax.swing.JFrame {

private static MyFrame instance;

public MyFrame(){
    initComponents();
    instance = this;
}


public static MyFrame newInstance() {
    if(instance == null){
        new MyFrame();
    }
    return instance;
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    textArea = new javax.swing.JTextArea();
    startButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    textArea.setEditable(false);
    textArea.setColumns(20);
    textArea.setRows(5);
    jScrollPane1.setViewportView(textArea);

    startButton.setText("Start");
    startButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1)
            .addContainerGap())
        .addGroup(layout.createSequentialGroup()
            .addGap(162, 162, 162)
            .addComponent(startButton)
            .addContainerGap(181, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(98, Short.MAX_VALUE)
            .addComponent(startButton)
            .addGap(39, 39, 39)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        



// Variables declaration - do not modify                     
private javax.swing.JButton startButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textArea;
// End of variables declaration  


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    Manager.getInstance().startMethod();
}


public void setTextArea(String str) {
    textArea.append(str + System.getProperty("line.separator"));
} 
}

1 个答案:

答案 0 :(得分:2)

你必须像这样对你的startMethod()进行线程化:

public void startMethod() {
    new Thread() {
        public void run() {
            for (int i = 0; i < 5000; i++) {
                doSmething();
                if (i == 0) {
                    sendMsg("Start");
                }

            }
            sendMsg("End");
        }
    }.start();
}

您的设计存在以下问题: AWT-EventQueue会触发ButtonEvent并创建5000个Threaded对象。 这些将运行并更新TextArea,但TextArea将仅在AWT-Eventqueue-Thread发布后重新绘制(在创建所有5000个对象之后)

你不应再需要线程化你的MyFrameMsg类了(除了它们应该做的不仅仅是将来的简短更新)