我尝试使用Swing显示消息,但是当我创建它时,我收到此错误消息:
线程中的异常" AWT-EventQueue-0" java.awt.HeadlessException异常 at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204) 在java.awt.Window。(Window.java:536) 在java.awt.Frame。(Frame.java:420) 在java.awt.Frame。(Frame.java:385) 在javax.swing.SwingUtilities $ SharedOwnerFrame。(SwingUtilities.java:1758) 在javax.swing.SwingUtilities.getSharedOwnerFrame(SwingUtilities.java:1833) 在javax.swing.JOptionPane.getRootFrame(JOptionPane.java:1696) 在javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:863) 在javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:666) 在javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:637) 在javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:608) 在sintel.onetouch.online.controle.ControlePrincipal $ 1.run(ControlePrincipal.java:272) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access $ 500(EventQueue.java:97) at java.awt.EventQueue $ 3.run(EventQueue.java:709) at java.awt.EventQueue $ 3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method)
这是代码的一部分:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
List<Evento> events = null;
while(true)
{
try
{
if(codigoUltimoEvento <= 0)
{
codigoUltimoEvento = controleAcessoDB.buscaCodigoUltimoEvento();
System.out.println("codigo ultimo evento:" + codigoUltimoEvento);
}
events = controleAcessoDB.listaEvento(codigoUltimoEvento);
if (events.size() > 0)
{
Evento evento = events.get(events.size() - 1);
codigoUltimoEvento = evento.getCodigoEvento();
System.out.println("codigo ultimo evento novo:" + codigoUltimoEvento);
}
else
{
try
{
sleep(4000);
}
catch (InterruptedException ex)
{
GerenciadorLog.logException(ex);
Logger.getLogger(ControlePrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
continue;
}
}
catch (Exception ex)
{
ex.printStackTrace();
GerenciadorLog.logException(ex);
events = null;
}
if(events != null)
{
System.out.println("//eventos != null");
//TODO porque i = 1?
int i = 1;
for (Evento evento : events)
{
i = ++i;
if(evento.getEquipamento().getTipoComunicacao() != ETipoComunicacao.IP_UDP)
{
continue;
}
if (((evento.getTipoEvento() == ETipoEvento.ACESSO_EMERGENCIA) || evento.getTipoEvento() == ETipoEvento.SAIDA_EMERGENCIA))
{
String usarSom = GerenciadorProperties.getProperty("som.ativo");
JOptionPane.showMessageDialog(null, "Eggs are not supposed to be green.");
}
}
}
try
{
sleep(5000);
}
catch (InterruptedException ex)
{
GerenciadorLog.logException(ex);
Logger.getLogger(ControlePrincipal.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
我在netbeans IDE上的Windows 10计算机上运行它。
我已经尝试过使用JavaFX而不是Swing,但它给了我其他问题。 我尝试在一个新线程上执行,使用类似上面代码的swing worker。