Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 快给绝地武士写信_Java_Multithreading_Swing_Jeditorpane_Invokelater - Fatal编程技术网

Java 快给绝地武士写信

Java 快给绝地武士写信,java,multithreading,swing,jeditorpane,invokelater,Java,Multithreading,Swing,Jeditorpane,Invokelater,在下面的代码中,我想把数字0到10000写在JEditorPane上。但是,除非完全完成(一次打印所有0到10000),或者操作系统给它时间刷新和显示内容,否则JEditorPane不会显示任何内容。无论哪种情况,应用程序都会在一段时间内失去响应,用户认为应用程序已经崩溃。我们是否可以强制jEditorPane显示更新的内容,即使它仍然很忙?我尝试了invokeAndWait,但在这里没有帮助,因为我们无法从事件调度程序线程调用invokeAndWait 换句话说,如果我将Thread.slee

在下面的代码中,我想把数字0到10000写在JEditorPane上。但是,除非完全完成(一次打印所有0到10000),或者操作系统给它时间刷新和显示内容,否则JEditorPane不会显示任何内容。无论哪种情况,应用程序都会在一段时间内失去响应,用户认为应用程序已经崩溃。我们是否可以强制jEditorPane显示更新的内容,即使它仍然很忙?我尝试了invokeAndWait,但在这里没有帮助,因为我们无法从事件调度程序线程调用invokeAndWait

换句话说,如果我将
Thread.sleep(0)
替换为
Thread.sleep(50)
,它可以正常工作,并在结果出现时显示打印结果,但是添加50毫秒的延迟会使速度非常慢。我只想不时更新JEditorPane,这样用户就不会认为应用程序崩溃了我只希望修改updateMessages()。 这是我的密码:

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class CMessageWindowStack {

    private static final String MESSAGE = "msg";
    private  JScrollPane scrollPane;
    public  JEditorPane  editorPane;
    private  HTMLEditorKit kit;
    private  String msgBuffer=new String("");
    private static CMessageWindowStack window=null;
    private static JFrame frameContainer=null;

    private CMessageWindowStack()
    {
        editorPane  = new JEditorPane ();
        editorPane.setEditable(false);
        editorPane.setContentType("text/html");
        kit = new HTMLEditorKit();
        editorPane.setEditorKit(kit);

        StyleSheet styleSheet = kit.getStyleSheet();
        styleSheet.addRule("."+MESSAGE+" {font: 10px monaco; color: black; }");

        Document doc = kit.createDefaultDocument();
        editorPane.setDocument(doc);
        scrollPane = new JScrollPane(editorPane);
    }
    public static CMessageWindowStack getInstance(){
        if (null==window)
        {window=new CMessageWindowStack();}
        return window;
    }
/**
 * The core
 * @param sMessage
 * @param sType
 */
    private void updateMessages(final String sMessage, final String sType)

    {
        SwingUtilities.invokeLater( new Runnable() {
             public void run() {

        String sMessageHTML=""; 
        String sTypeText="";
        if (!sMessage.equals("\r\n")){ 
            sTypeText = sType+": ";
        }

        sMessageHTML = sMessage.replaceAll("(\r\n|\n)", "<br/>");
        if (!sMessageHTML.equals("<br/>")) 
        {
            sMessageHTML =   "<SPAN CLASS="+sType+">"+ sTypeText+sMessageHTML + "</SPAN>";
        }

        msgBuffer=msgBuffer.concat( sMessageHTML);
        editorPane.setText(msgBuffer);
        if ((editorPane.getDocument()).getLength()>1){
            editorPane.setCaretPosition((editorPane.getDocument()).getLength()-1);
        }  
             }
          });
    }

    public void setContainerFrame(JFrame jFrame){
        frameContainer = jFrame;
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frameContainer.getContentPane());
        frameContainer.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(scrollPane)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                );
    }

    public void setVisible(boolean bVisible){
        editorPane.setVisible(bVisible);
        scrollPane.setVisible(bVisible);
    }

    public void printMsg(String sMessage){
        String sType = MESSAGE;
        updateMessages(sMessage,sType);
    }

    public void printlnMsg(String sMessage){
        sMessage=sMessage.concat("\r\n");
        printMsg(sMessage);
    }


    public static void main(String args[]){
        CMessageWindowStack m_LogMgr;
        JFrame frame = new JFrame();
        m_LogMgr=CMessageWindowStack.getInstance();
        m_LogMgr.setContainerFrame(frame);
        frame.setVisible(true);
        frame.setSize(500, 500);


        for(int i=0;i<10000;++i){
            try {
                Thread.sleep(0);//becomes unresponsive if sleep(0)
            } catch (Exception e) {
            }
            m_LogMgr.printlnMsg(i+"-----------------------");
        }

    }


}
import javax.swing.JEditorPane;
导入javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.SwingUtilities;
导入javax.swing.text.Document;
导入javax.swing.text.html.HTMLEditorKit;
导入javax.swing.text.html.StyleSheet;
公共类CMessageWindowStack{
私有静态最终字符串MESSAGE=“msg”;
私有JScrollPane滚动窗格;
公共去离子去氧丙烷;
私人HTMLeditor工具包;
私有字符串msgBuffer=新字符串(“”);
私有静态CMessageWindowStack window=null;
私有静态JFrame frameContainer=null;
私有CMessageWindowStack()
{
editorPane=新的JEditorPane();
editorPane.setEditable(false);
setContentType(“text/html”);
kit=新的HTMLEditorKit();
editorPane.setEditorKit(工具包);
StyleSheet StyleSheet=kit.getStyleSheet();
addRule(“.”+消息+”{font:10px;颜色:黑色;}”);
Document doc=kit.createDefaultDocument();
editorPane.setDocument(doc);
scrollPane=新的JScrollPane(editorPane);
}
公共静态CMessageWindowStack getInstance(){
如果(空==窗口)
{window=new CMessageWindowStack();}
返回窗口;
}
/**
*核心
*@param sMessage
*@param sType
*/
私有void updateMessages(最终字符串sMessage、最终字符串sType)
{
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
字符串sMessageHTML=“”;
字符串sTypeText=“”;
如果(!sMessage.equals(“\r\n”){
sTypeText=sType+“:”;
}
sMessageHTML=sMessage.replaceAll(“(\r\n |\n)”,“
”; 如果(!sMessageHTML.equals(“
”) { sMessageHTML=“”+StypText+sMessageHTML+”; } msgBuffer=msgBuffer.concat(sMessageHTML); editorPane.setText(msgBuffer); if((editorPane.getDocument()).getLength()>1){ editorPane.setCaretPosition((editorPane.getDocument()).getLength()-1); } } }); } 公共void setContainerFrame(JFrame JFrame){ frameContainer=jFrame; javax.swing.GroupLayout=newjavax.swing.GroupLayout(frameContainer.getContentPane()); frameContainer.getContentPane().setLayout(布局); layout.setHorizontalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(滚动窗格) ); layout.setVerticalGroup( createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(滚动窗格,javax.swing.GroupLayout.DEFAULT_SIZE,217,Short.MAX_VALUE)) ); } public void setVisible(布尔值bVisible){ editorPane.setVisible(bVisible); scrollPane.setVisible(bVisible); } public void printMsg(字符串消息){ 字符串sType=消息; 更新消息(sMessage、sType); } public void printlnMsg(字符串消息){ sMessage=sMessage.concat(“\r\n”); printMsg(sMessage); } 公共静态void main(字符串参数[]){ CMessageWindowStack m_LogMgr; JFrame=新JFrame(); m_LogMgr=CMessageWindowStack.getInstance(); m_LogMgr.setContainerFrame(框架); frame.setVisible(true); 框架。设置尺寸(500500);
对于(int i=0;i您可能需要使用SwingWorker。首先用基本HTML加载JEditorPane。然后您需要
发布每行数据。发布数据后,您需要将文本插入编辑器窗格

基本思想是不能一次创建整个HTML字符串


我一直无法真正理解如何在动态更改HTML的情况下使用JEditorPane。也许你可以只使用JTextPane。它支持不同的字体和颜色,并且更易于动态更新。

作为黑客,你可以尝试以下方法:

    for(int i=0;i<10000;++i){
        try {
            if( i%100 == 0 ) {
                Thread.sleep(10);//becomes unresponsive if sleep(0)
            }
        } catch (Exception e) {
        }
        m_LogMgr.printlnMsg(i+"-----------------------");
    }

对于(int i=0;i您可以尝试使用线程并赋予其最高优先级。此外,您还可以向java.exe添加-XmX2020选项


另一个解决方案可能是,如果你使用JTextArea而不是
jedorpane
。似乎你对HTML所做的只是设计字体。你可以使用JTextArea上的
java.awt.Font
属性来做这件事。

+1.
SwingWorker
是正确的答案。睡眠几乎不是正确的答案。谢谢,但我一直在寻找一些解决方案只需要在updateMessages内部修改的选项。然后,解决方案是一个相当大的努力与线程,如其他答案中所建议的。我提供了这个黑客,因为你似乎在玩睡眠呼叫。可能是一些提示可以帮助