gpt4 book ai didi

java - JOptionPane 给出错误

转载 作者:行者123 更新时间:2023-12-02 09:23:14 26 4
gpt4 key购买 nike

我想使用 JOptionPane 处理一些异常。这是主要方法:

public class MainRun {

public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(new SubstanceRavenGraphiteGlassLookAndFeel());
}catch (Exception e){
e.printStackTrace();
}
new MainGui().setVisible(true);
new Initialized().start();
new PriorityMessageQueue().start();
}
});

MainGui 是应用程序的主窗口 (JFrame)。我在 PriorityMessageQueue 线程内处理了异常。

public class PriorityMessageQueue extends Thread {

@Override
public void run() {

while (true) {
try {
instantMessages = instant.getMobitelMessagesToBeSent();
} catch (Exception ex) {
JOptionPane.showMessageDialog(
null,
ex.getMessage(),
"Database Error",
JOptionPane.ERROR_MESSAGE);

}

...

运行此命令后,出现错误

   org.jvnet.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
at org.jvnet.substance.utils.SubstanceCoreUtilities.testComponentCreationThreadingViolation(SubstanceCoreUtilities.java:2312)
at org.jvnet.substance.SubstanceOptionPaneUI.createUI(SubstanceOptionPaneUI.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:37)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:244)
at javax.swing.UIDefaults.getUI(UIDefaults.java:752)
at javax.swing.UIManager.getUI(UIManager.java:989)
at javax.swing.JOptionPane.updateUI(JOptionPane.java:1859)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1822)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1785)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1753)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1731)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1711)
at GUI.MainGui.showOptionPane(MainGui.java:1039)
at SMS.PriorityMessageQueue.run(PriorityMessageQueue.java:86)
UIDefaults.getUI() failed: createUI() failed for
javax.swing.JOptionPane
[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=0,maximumSize=,minimumSize=,p
referredSize=,icon=,initialValue=,message=DatabaseConnection class
connect,messageType=ERROR_MESSAGE,optionType=DEFAULT_OPTION,wantsInput=false]
java.lang.reflect.InvocationTargetException
java.lang.Error
at javax.swing.UIDefaults.getUIError(UIDefaults.java:712)
at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
at javax.swing.UIDefaults.getUI(UIDefaults.java:758)
at javax.swing.UIManager.getUI(UIManager.java:989)
at javax.swing.JOptionPane.updateUI(JOptionPane.java:1859)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1822)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1785)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1753)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1731)
at javax.swing.JOptionPane.<init>(JOptionPane.java:1711)
at GUI.MainGui.showOptionPane(MainGui.java:1039)
at SMS.PriorityMessageQueue.run(PriorityMessageQueue.java:86)

并且 JOptionPane 将无法正确显示!

enter image description here

如何获取parentComponent (MainGui JFrame),而不是将null传递给JOptionPane?/p>

最佳答案

JOptionPane 是一个迷你 Swing GUI,与所有 Swing GUI 一样,它必须在 Swing 事件线程上创建,而不是在后台线程上创建。这可以通过在 Runnable 中创建 JOptionPane,然后在事件线程上对 Runnable 进行排队来完成:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(....);
}
});

例如,

} catch (Exception ex) {
final String exMessage = ex.getMessage();

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(
null,
exMessage,
"Database Error",
JOptionPane.ERROR_MESSAGE);
);
}
});
}
}

关于你的问题,

Instead of passing null to JOptionPane, how can I get the parentComponent (MainGui JFrame)?

您需要主 GUI 的引用。如何获得此信息取决于您的程序的组织方式。

关于java - JOptionPane 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186842/

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