是什么导致我的基于 JTabbedPane 的 Swing 应用程序性能下降? [英] What is causing slow performance in my JTabbedPane-based Swing application?

查看:54
本文介绍了是什么导致我的基于 JTabbedPane 的 Swing 应用程序性能下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于创建 RPG 角色的 Swing 应用程序.它有一个嵌套的 JTabbedPane 架构,所以有一个 HeroTabsPanelHeroPanels 并且每个都有更多的选项卡,如统计、项目等.

I have a Swing application for creating RPG characters. It has a nested JTabbedPane architecture, so there is a HeroTabsPanel which has HeroPanels and each of those has some more tabs like Stats, Items etc.

所以 GUI 由上层的英雄标签面板、下层当前英雄标签的标签面板和一个 EditViewPanel 组成,该面板显示一个 EditingView 对应于每个标签的 EditingView选项卡被选中.

So the GUI is comprised of the upper tabbed pane for heroes, the lower tabbed pane for current hero tab and an EditViewPanel which displays an EditingView corresponding to each Tab when the tab is selected.

性能从一开始就很糟糕,但是当我添加上层英雄标签(同时编辑多个英雄)时,标签之间的切换变得更慢.在所有代码完成添加组件后,需要几分钟才能在新 JFrame 中显示某些内容.这可能是布局吗?

Performance has been bad from the start, but when I added the upper level hero-tabs (to have multiple heroes in editing simultaneously), switching between tabs became even slower. It takes a few minutes for something to be displayed in the new JFrame after all the code has finished adding components. Could this be the layout?

我使用具有绝对定位的 MigLayout.实际上,在我添加上面的选项卡之前,存在一些绝对链接值中的不稳定循环依赖!" 问题,但现在不知何故.

I am using MigLayout with absolute positioning. Actually, before I added upper tabs, there had been some "Unstable Cyclic Dependency in absolute-linked values!" issues, but now there aren’t somehow.

stateChanged() 中,我有以下内容:

In stateChanged() I have what amounts to this:

editViewPanel.activateView(currentTab.getLinkedView());

activateView() 中:

removeAll();
currentView = heroView;
add(currentView, "pos 0 0");
currentView.refresh();
revalidate();

但是就像我说的,所有的代码执行都在合理的时间内完成,我已经完成了我的分析,但是在完成之后,有相当长的延迟,在第一次添加的情况下长达几分钟到一个新的 JFrame.

But like I said, all the code execution is finished in reasonable time, I've done my profiling, but after it is done, there a delay of considerable length, up to a few minutes in the case of first time adding to a new JFrame.

另一个问题是,当我需要更新下部选项卡窗格中的面板时,尤其是 StatsPanel,它由为几列中的每个参数添加的 string-int 元素组成,我又得到了很大的延迟.它还取决于 MigLayout 并且出于某种原因(我知道糟糕的设计)也具有绝对定位.但是我已经更改了它,以便在初始化后不删除/添加任何组件,只使用 setText() 并且在代码完成执行后仍然有很大的延迟.

Another issue is that when I need to update a panel within the lower tabbedpane, especially StatsPanel which is made up of string-int elements added for each parameter in a few columns, I get yet another large delay. It also depends on MigLayout and for some reason (bad design, I know) has absolute positioning as well. But I have changed it so that no components are removed/added after initialization, only setText() is used and still there is a big delay after the code is finished executing.

我打算使用 SwingWorkers,但我想在开始解决问题之前更好地了解问题.我怀疑这很简单,但我有点怀疑它造成的延迟有多大.我很感激有关 SwingWorkers 的一些提示/示例.

I’m planning to use SwingWorkers but I would like to understand the problem better before I start solving it. I suspect it's simple, but I am a bit incredulous about how big the delays it's causing are. I'd appreciate some hints/examples about SwingWorkers.

如果您对问题可能隐藏的地方有大致了解,我可以添加更多代码.

I can add more code if you have some general idea where the issue might hide.

欢迎提出任何建议,谢谢!

Any suggestions are welcome, thanks!

推荐答案

好的,我终于通过 EDT 转储解决了这个问题.冻结由于布局,虽然看起来不太可能.每次选择新选项卡时,MigLayout 都试图找出所有组件的大小,并且可能针对 init 上所有选项卡中的所有组件.解决方案是简单地为 JTabbedPanel 实现覆盖 getPreferredSize().@Robin,感谢线程转储提示!

Ok, I finally worked it out by going through the EDT dump. The freeze was due to layout, unlikely though it had seemed. MigLayout was trying to figure out the sizes of all the components every time new tab was selected, and probably for all the components in all the tabs on init. The solution is simply to override getPreferredSize() for the JTabbedPanel implementation. @Robin, thanks for the thread dump hint!

这篇关于是什么导致我的基于 JTabbedPane 的 Swing 应用程序性能下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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