BorderLayout 延迟问题 [英] BorderLayout delay issues

查看:27
本文介绍了BorderLayout 延迟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过很长一段时间后,我确定如果文本区域处于边框布局中,那么在文本区域中快速显示文本时会出现延迟.我的问题是,为什么使用边框布局执行以下代码的时间比不使用边框布局的时间长 10-20 倍(注释掉 addWithBorderLayout 或 addWithoutBorderLayout 这两种方法之一)并且有没有办法在没有这种延迟的情况下使用边框布局?(无论是否使用 SwingUtilities invokeLater() 方法,问题都存在.)

After a very long couple of days I have determined that there is a delay when displaying text very quickly in a text area if that text area is in a border layout. My question is, why does the following code take 10-20 times longer to execute using border layout than without (comment out one of the two methods either addWithBorderLayout or addWithoutBorderLayout) AND is there a way to use the border layout without this delay? (The problem exists with or without the SwingUtilities invokeLater() method.)

import java.awt.BorderLayout;
import java.awt.Label;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;


public class Driver
{
    public static void main(String args[])
    {
        System.out.println("JEditPane Test");

        //window to display the plyed back text
        JFrame mainFrame = new JFrame("Main Frame");

        //holds some text to be played back
        final JEditorPane editPane1 = new JEditorPane(); 
        final JEditorPane editPane2 = new JEditorPane(); 

        //desktop pane to hold docs
        JDesktopPane desktopPane = new JDesktopPane();

        //create an internal frame
        JInternalFrame internalFrame1 = new JInternalFrame("Test Doc 1", true, true, true, true);
        internalFrame1.setContentPane(new JScrollPane(editPane1));
        internalFrame1.setSize(400, 400);
        internalFrame1.setVisible(true);
        internalFrame1.setLocation(0, 0);

        JInternalFrame internalFrame2 = new JInternalFrame("Test Doc 2", true, true, true, true);
        internalFrame2.setContentPane(new JScrollPane(editPane2));
        internalFrame2.setSize(400, 400);
        internalFrame2.setVisible(true);
        internalFrame2.setLocation(400, 0);

        //add it to the desktop
        desktopPane.add(internalFrame1);
        desktopPane.add(internalFrame2);

        //map of editor panes
        final Map < String, JEditorPane > mapOfPanes = new HashMap < String, JEditorPane >();
        mapOfPanes.put("1", editPane1);
        mapOfPanes.put("2", editPane2);

        //COMMENT ONE OF THESE TWO OUT!!!
        addWithBorderLayout(mainFrame, desktopPane);
        //addWithoutBorderLayout(mainFrame, desktopPane);

        //for closing
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //set the size and location of the window
        mainFrame.setSize(800,500);
        mainFrame.setLocation(100, 100);

        //make the window visible
        mainFrame.setVisible(true);


        //create some text to display
        StringBuilder builder = new StringBuilder();
        builder.append("This is a rather long string of text. ");

        //build up a good amount of text
        for(int i = 0;i < 5;i++)
        {
            //copy it a few times
            builder.append(builder.toString());
        }

        //get the string
        final String longStringOfText = builder.toString();

        //create a thread to call setText on the editor pane
        Thread thread = new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                //for gathering stats
                int sum = 0;
                int numberOfCharsToPrintFromString = 0;
                Date prev = new Date();
                Date current = new Date();

                System.out.println("Num Panes: " + mapOfPanes.size());

                //for each pane
                for(JEditorPane pane : mapOfPanes.values())
                {
                    //to help in printing subsections of the big string
                    numberOfCharsToPrintFromString = 0;

                    while(numberOfCharsToPrintFromString < longStringOfText.length())
                    {
                        //wait a short amount of time
                        try{Thread.sleep(1);}catch(Exception e){}

                        //grab sections of the long string
                        String text = longStringOfText.substring(0, numberOfCharsToPrintFromString);

                        //set the text of the pane
                        pane.setText(text);

                        //stats
                        numberOfCharsToPrintFromString++;
                        long diff = current.getTime() - prev.getTime();
                        sum = sum + (int)diff;
                        prev = current;
                        current = new Date();
                    }               
                }

                System.out.println("Average time in between events: " + ((double)sum/(double)numberOfCharsToPrintFromString));                  
            }
        });

        thread.start();
    }

    private static void addWithoutBorderLayout(JFrame mainFrame, JDesktopPane desktopPane)
    {
        mainFrame.add(desktopPane);
    }

    private static void addWithBorderLayout(JFrame mainFrame, JDesktopPane desktopPane)
    {
        mainFrame.setLayout(new BorderLayout());
        mainFrame.add(new Label("Top Panel"), BorderLayout.NORTH);
        mainFrame.add(desktopPane, BorderLayout.CENTER);
        mainFrame.add(new Label("Bottom Panel"), BorderLayout.SOUTH);       
    }
}

推荐答案

在使用 Java 1.6 版的 Mac OS X 10.5.8 上,除非我设置 apple.awt.graphics.UseQuartz 在 main() 的开头.这是一个相关的示例,它影响字体渲染质量而不是执行时间.

On Mac OS X 10.5.8 using Java version 1.6, I see a comparable disparity unless I set apple.awt.graphics.UseQuartz at the beginning of main(). Here is a related example that affects font rendering quality rather than execution time.

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    System.setProperty("apple.awt.graphics.UseQuartz", "true");
}

这篇关于BorderLayout 延迟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆