gpt4 book ai didi

java - validate() 树在 L&F 更改时抛出 NullPointerException?

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:52 24 4
gpt4 key购买 nike

public class TabbedArea extends JTabbedPane {
public void addArea(){
add(component);
final JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(title), BorderLayout.LINE_START);
JButton closeButton = new JButton(new CloseIcon());
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeArea(subAreas.get(indexOfTabComponent(panel)));
}
});
panel.add(closeButton, BorderLayout.LINE_END);

setTabComponentAt(getTabCount() - 1, panel);
}
}

public class LnFManager {
public void setTheme(PlasticTheme theme){
for (Window window : Window.getWindows()) {
if (window.isDisplayable())
SwingUtilities.updateComponentTreeUI(window);
}
}
}

当调用 setTheme() 时,这会导致重复发生(大概是针对每个组件):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTabbedPaneUI.scrollableTabLayoutEnabled(BasicTabbedPaneUI.java:263)
at javax.swing.plaf.basic.BasicTabbedPaneUI.access$400(BasicTabbedPaneUI.java:54)
at javax.swing.plaf.basic.BasicTabbedPaneUI$TabContainer.doLayout(BasicTabbedPaneUI.java:3850)
at java.awt.Container.validateTree(Container.java:1568)
at java.awt.Container.validateTree(Container.java:1575)
at java.awt.Container.validateTree(Container.java:1575)
at java.awt.Container.validateTree(Container.java:1575)
at java.awt.Container.validateTree(Container.java:1575)
at java.awt.Container.validate(Container.java:1540)
at javax.swing.SwingUtilities.updateComponentTreeUI(SwingUtilities.java:1227)
at flextrade.tca.ui.LnFManager.setTheme(LnFManager.java:113)
at flextrade.tca.ui.ToolBar$15.actionPerformed(ToolBar.java:803)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
at javax.swing.AbstractButton.doClick(AbstractButton.java:374)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:829)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:873)
at java.awt.Component.processMouseEvent(Component.java:6108)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:5873)
at java.awt.Container.processEvent(Container.java:2105)
at java.awt.Component.dispatchEventImpl(Component.java:4469)
at java.awt.Container.dispatchEventImpl(Container.java:2163)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055)
at java.awt.Container.dispatchEventImpl(Container.java:2149)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4295)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:604)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

注释掉“setTabComponentAt(getTabCount() - 1, panel)”这一行;阻止抛出异常(但这当然会破坏 UI),就像在 TabbedArea 类中覆盖 updateUI() 并将 super.updateUI() 包装在 SwingUtilities.invokeLater(..) 中一样。

这似乎类似于 bug jEdit 团队在更新 L&F 时使用了他们的一个插件。然而,他们的solution只是在运行时不更新应用程序的外观,而是依靠重启来完成。

任何人都知道如何在不需要重新启动来改变外观的情况下解决这个问题?

最佳答案

查看 Java 6 的源代码,我发现 BasicTabbedPaneUI 的第 263 行是一个返回语句:

        /* In an attempt to preserve backward compatibility for programs
* which have extended BasicTabbedPaneUI to do their own layout, the
* UI uses the installed layoutManager (and not tabLayoutPolicy) to
* determine if scrollTabLayout is enabled.
*/
private boolean scrollableTabLayoutEnabled() {
return (tabPane.getLayout() instanceof TabbedPaneScrollLayout);
}

唯一可能导致 NPE 的是 tabPane 引用为空。在没有更多信息的情况下,我只能猜测您已经删除了一个选项卡组件,但没有删除相应的选项卡。

关于java - validate() 树在 L&F 更改时抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4217741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com