gpt4 book ai didi

java - 抛出 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 10:01:56 28 4
gpt4 key购买 nike

我正在尝试使用 SwingUtilities 创建 JFrame

Thread tt = new Thread(new Runnable()
{
public void run()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{

@Override
public void run()
{
new Loadingframe();

}
});
} catch (InvocationTargetException e)
{
e.printStackTrace();
} catch (InterruptedException e)
{
e.printStackTrace();
}

}
});
tt.start();

类Loadingframe是一个非常简单的JFrame。有时,在运行此代码时,我会在

中收到 ClassCastException
new Loadingframe();

我还尝试了 InvokeLaterThreads only without Swing 但也出现同样的错误

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(LayoutComparator.java:75)
at javax.swing.LayoutComparator.compare(LayoutComparator.java:42)
at java.util.TimSort.binarySort(TimSort.java:292)
at java.util.TimSort.sort(TimSort.java:217)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.ArrayList.sort(ArrayList.java:1454)
at java.util.Collections.sort(Collections.java:175)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:136)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:110)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:445)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:166)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:535)
at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:169)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:420)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:128)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

以及我的加载类

public class Loadingframe extends JFrame
{
public Loadingframe()
{
this.setType(Type.UTILITY);
this.setUndecorated(true);
setBackground(new Color(0, 0, 0, 0));
setSize(new Dimension(500, 500));

setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
JButton b = new JButton(
new ImageIcon(getClass().getResource("/reso/loading11.png")));
b.setBorderPainted(false);
b.setFocusPainted(false);
b.setContentAreaFilled(false);
add(b);
this.setVisible(true);
this.addWindowFocusListener(new WindowFocusListener()
{

@Override
public void windowLostFocus(WindowEvent e)
{
Loadingframe.this.dispose();
}

@Override
public void windowGainedFocus(WindowEvent e)
{
}
});
this.setFocusable(true);
getContentPane().requestFocus();
}
}

最佳答案

我发现您还没有设置平台外观或感觉?

像这样

try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) { }

间歇性错误有时是由于 Swing 组件未在 Swing 维护的组件层次结构上注册而导致的。通常,当我们在 EDT(Event Dispatcher Thread)之外的其他线程上加载 GUI 内容时,就会发生这种情况。为了确保您注册组件,请执行以下操作,

 Thread tt = new Thread(new Runnable()
{
public void run()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{

@Override
public void run()
{
EventQueue.invokeLater(new Runnable() { public void run() {
new Loadingframe();

}});

}
});
} catch (InvocationTargetException e)
{
e.printStackTrace();
} catch (InterruptedException e)
{
e.printStackTrace();
}

}
});

关于java - 抛出 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742067/

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