Swing - 缓慢的UI更新和修复

时间:2015-12-07 06:52:52

标签: java multithreading swing thread-safety

我的JPanel ButtonMenu在一段时间内的UI更新速度很慢(可以说有时在第四次点击时有时候是20)。问题很简单,这幅画刚刚推迟。所以我做了很多阅读并遇到了很多建议,特别是thisthisthis。但似乎没有什么能解决它。所以我的问题是

  1. 何时使用以下参数修复应用程序?以及如何检查它们是否在系统中生效了?

    Dsun.java2d.xrender Dsun.java2d.pmoffscreen Dsun.java2d.d3d

  2. 这曾经在java 8之前运行良好。但我不能肯定地说java 8是否有问题,因为它有时会起作用。渲染打破了一段时间的使用,这也是对观察的判断。当它被推迟时,它只是不会更新,直到其他东西唤醒它。

  3. 更新:

    所以我找到了真正的问题。我填充按钮并进行UI更新的方法如下:

    public void showButtonMenuList(final MenuList menulist)
    {
        GuiExecutor.getInstance().update(new Runnable()
        {
            public void run()
            {
                notifyListenersOfButtonMenuShow();
                buttonMenu = ButtonMenu.makeButtonMenu(menulist.getMenuOptions(), MyBigPanel.this);
                leftSidePanel.removeAll();
                leftSidePanel.add(buttonMenu.getPanel(), BorderLayout.CENTER);
            }
        });
        freshup();
    }
    

    notifyListenersOfButtonMenuShow()是:

    public void notifyListenersOfButtonMenuShow(){
            Object[] ButtonMenuListenerslist = ButtonMenuListeners.toArray();
            for (int ButtonMenuListenerslistIterator = 0;ButtonMenuListenerslistIterator < ButtonMenuListenerslist.length;ButtonMenuListenerslistIterator++){
                ((ButtonMenuListener) ButtonMenuListenerslist[ButtonMenuListenerslistIterator]).menuShowing();
            }
    }

    此处 freshup()是:

    protected void freshup()
    {
        panel.repaint();
        panel.revalidate();
    }
    

    update()属于我的GuiExecutor类,如下所示:

    public void update(final Runnable runnable)
    {
        if (SwingUtilities.isEventDispatchThread())
        {
            runnable.run();
        }
        else
        {
            try
            {
                 SwingUtilities.invokeLater(runnable);
            }
            catch (Exception ex)
            {}
        }
    }
    

    因此,如果运行中的任何内容都被延迟,则freshup()将会被执行。围绕这个的最好方法是什么,而不是在run()中调用freshup()。可能是一种方法来修复不一致的延迟或更好的设计模式,以使这个线程安全吗?

0 个答案:

没有答案