java - Java 物质外观和感觉的 GUI 问题

标签 java swing user-interface look-and-feel

设置外观时出现一个非常奇怪的错误。为了设置外观,我使用以下内容:


... String scheme = "net.sourceforge.atunes.gui.substance.SubstanceATunesSunLookAndFeel";
try {
            UIManager.setLookAndFeel(scheme);
            UIManager.put(LafWidget.ANIMATION_KIND, LafConstants.AnimationKind.NONE);
            UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CONTENT_BORDER_KIND, SubstanceConstants.TabContentPaneBorderKind.SINGLE_FULL);

            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
        } catch (ClassNotFoundException e) {
            ExceptionHandler.handleSilently(e);
        } catch (InstantiationException e) {
            ExceptionHandler.handleSilently(e);
        } catch (IllegalAccessException e) {
            ExceptionHandler.handleSilently(e);
        } catch (UnsupportedLookAndFeelException e) {
            ExceptionHandler.handleSilently(e);
        }

它被放置在主函数中:


SwingUtilities.invokeAndWait(new Runnable(){
                public void run(){
                    ...
                }

在设置外观之前没有 gui 元素,因此不需要执行 SwingUtilities.updateComponentTreeUI(...)。所以,一切都很好,但是一些用户报告了非常奇怪的包,其中包含未处理的窗口,例如: 启动程序时,用户会看到以下屏幕(仅当他将鼠标移到该区域上时才会出现按钮;在此之前,窗口不会显示这些按钮。

<小时/>

那么,有人可以帮助我找到正确的解决方案吗(我不问解决方案,我只是问解决问题的正确方法)。首先,我认为这是因为内存不足错误而发生的,但用户计算机的配置是:

Machine configuration:
HP d530 CMT(DF373A)
Windows 7 Ultimate, 32Bit,SP1
2GB Ram
NVIDIA GeForce FX 5700 (1680x1050, 32Bit Col depth)
Java 1.6.0_26

So, I guess Out Of Memory is not the case. Any suggestions, pls.


UPD: so, every GUI creating statements were moved to one SwingUtilities.invokeLater() statement in main function! But, the problem is still reproduced by some users. Also, it's now known, that only view is so weird, but every buttons on it behave as expected! (I mean after pressing Ok button, next MVC is showed and looks well). This bug happens only with very first window which is created right after setting look-and-feel. So, I guess it's not the case of incorrect EDT usig, because of well buttons Listener's execution. Besides, our log (log4j) looks great as nothing weird happens!
Could anyone suggest possible cause?

SOLUTION see http://java.sun.com/products/java-media/2D/perf_graphics.html As commandline parameter where was added:


-Dsun.java2d.noddraw=true

最佳答案

同意@Joey

如果您有来自 Kirill 的 Java.net 的原始代码源,那么有很多示例可以向您展示

1)先安装UI

UIManager.installLookAndFeel("Substance " + sInfo.getDisplayName(), sInfo.getClassName());

SwingUtilities.invokeLater(new Runnable() {

   @Override
      public void run() {
           someMainclass.setVisible(true);
      }
}); 

2)您在 try - catch 中打包的所需 SwingUtilities.updateComponentTreeUI(someWindow) 的问题

for (Window w : Window.getWindows()) {
  SwingUtilities.updateComponentTreeUI(w);
}

3) 或者最安全的方式,确保提取 2dn 点的顶级容器

SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
                SwingUtilities.updateComponentTreeUI(frame);
            } catch (UnsupportedLookAndFeelException e) {
                throw new RuntimeException(e);
            }
        }
    });
    SwingUtilities.invokeLater(new Runnable() { 

4) 您可以切换主题、L&f、混合主题,但必须包含在 InvokeLater()

5)

编辑:

@rauch 正如我所提到的,Substance 对 EDT 非常敏感,在没有对 EDT 及其单线程规则有最深入了解的情况下,真的忘记了从 BackGround 任务中替换模型,

嗯,我尝试直接使用替换模型来避免一些 a​​rrayIndexException 以及来自 Substance 的一些异常(我忘了那是几个月前的事了),

你永远不会用 javax.swing.Timer 或 SwingWorker 愚弄这个问题,有时(我认为 HightLighter ??? 或 Trident.jar 中的某些东西拒绝正常工作 ??? 并 overRun EDT 队列)从来没有解决过这个问题的详细信息,

只是我将所有内容(从 Backgroung 任务输出到 GUI)包装到 AbstractAction

编辑2.当我阅读@kleopatra的评论时(我错误地忽略了她的建议)

would be my first guess as well - were it not Substance: usually it throws 
on not being on the EDT. Plus: s/he states that the first snippet is placed 
inside the invokeAndWait to let it run on the EDT

按照她的建议,Sustance 对于 SwingX 有自己的 L&f,对我来说 SwingX == kleopatra,我无法为您提供解决方法,因为我真的很讨厌 invokeAndWait 并且我避免使用此方法

关于java - Java 物质外观和感觉的 GUI 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6579087/

相关文章:

java - 如何在Intellij Java GUI设计器GridlayoutManager中制作等分单元格

Java UUID 压缩和解压缩

java - 使用 JRadioButtons 切换类似界面

java - 单击 "X"按钮时 JFrame 不会关闭

java - 双击编辑 Jtable

Python GUI登录程序

jquery - 借助 jQuery 实现的新 HTML 用户界面

java - 对齐数字时遇到问题

java - Spring Framework : Populating a Map<Enum, Object> with util:map

java - 安卓货币符号排序