2017-05-11 2 views
0

Nun, ich habe einen Wartebildschirm mit einer JDialog entwickelt, aber es funktioniert nur, wenn isoliert.GIF erscheint nicht korrekt auf JDialog

Hier ist mein Code:

/** 
* 
* @author krisnamourtscf 
*/ 
public class TelaDeProcessamento extends Thread { 

    private String titulo; 
    private String mensagem; 
    private JDialog dialog; 

    public TelaDeProcessamento(String titulo, String mensagem) { 
     this.titulo = titulo; 
     this.mensagem = mensagem; 
     dialog = new JDialog(new JFrame(), true); 
    } 



    public static TelaDeProcessamento iniciaTela(String titulo, String mensagem) { 
     TelaDeProcessamento tela = new TelaDeProcessamento(titulo, mensagem); 

     tela.start(); 
     return tela; 
    } 


    @Override 
    public void run() { 
     try { 

      dialog.setTitle(titulo); 
      dialog.getContentPane().setBackground(Color.WHITE); 
      dialog.setSize(new Dimension(300, 150)); 
      dialog.getContentPane().setLayout(new BorderLayout()); 

      ImageIcon ii = new ImageIcon(getClass().getResource("/imagens/4.gif")); 
      JLabel imageLabel = new JLabel(); 
      imageLabel.setLocation(70, 0); 
      imageLabel.setText("   " + mensagem); 
      imageLabel.setIcon(ii); 

      dialog.getContentPane().add(imageLabel, java.awt.BorderLayout.CENTER); 

      dialog.setLocationRelativeTo(null); 
      dialog.validate(); 
      dialog.setVisible(true); 

     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public void paraTela() { 
     dialog.dispose(); 
    } 

    public static void main(String[] args) { 
     TelaDeProcessamento tela=TelaDeProcessamento.iniciaTela("Aguarde....", "isso é um teste"); 
     try { 
      Thread.sleep(5000); 
     } catch (Exception ex) { 

     } 
     tela.paraTela(); 

    } 

} 

Und das ist das Ergebnis:

enter image description here

Obwohl, wenn ich es von einer anderen Klasse aufrufen, wird das GIF erscheinen nicht

Aus einer anderen Adresse JDialog Klassenbeispiel

this.setTitle(title); 
     this.setModal(true); 
     this.setLocationRelativeTo(null); 
     TelaDeProcessamento tela = TelaDeProcessamento.iniciaTela("Aguarde", "carregando dados"); 
     this.initiateScreenComponets(); 
     tela.paraTela(); 
     JTableUtil.addEventosSelecaoBusca(this); 
     this.setVisible(true); 

Ergebnis:

enter image description here

Was mache ich falsch?

+4

Ich vermute Dies liegt daran, dass Sie Ihre Aktionen außerhalb des AWT-Ereignisversand-Threads ausführen. Fast alle Swing- und AWT-Methoden müssen nur in diesem Thread und nicht in anderen Threads ausgeführt werden. Ein Verstoß gegen diese Regel führt zu merkwürdigem und unvorhersehbarem Verhalten. Siehe https://docs.oracle.com/javase/tutorial/uiswing/concurrency/. – VGR

+0

danke @VGR Ich löste das Problem mit SwingWorker – Krismorte

Antwort

0

Möglicherweise haben Sie ein Problem, das gif im Pfad zu finden ... verifizieren Sie das !!

Nachdem ich Ihren Code ausprobiert habe, kann ich bestätigen, dass Ihr Code funktionieren muss! enter image description here

+0

Es ist kein Pfadproblem – Krismorte

+0

Lesen Sie den obigen Kommentar, es ist wahrscheinlich ein Thading Problem @ Krismorte – Frakcool

+0

Ich habe beide versucht und funktioniert ... IT muss etwas mit dem Pfad verbunden sein –

0

ich neet nicht meine Klasse Methode nur die anrufende

@Override 
      public void initiate(String title) { 
    this.setTitle(title); 
    this.setModal(true); 
    this.setLocationRelativeTo(null); 
    TelaDeProcessamento tela = TelaDeProcessamento.iniciaTela("Aguarde", "carregando dados"); 
    this.initiateScreenComponets(); 
    tela.paraTela(); 
    JTableUtil.addEventosSelecaoBusca(this); 
    this.setVisible(true); 
} 

der neue Anruf

den alten Ruf

 @Override 
     public void initiate(String title) { 
      this.setTitle(title); 
      this.setModal(true); 
      this.setLocationRelativeTo(null); 
      procedimentoDeEspera(); 
     } 

    private void procedimentoDeEspera() { 
     SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { 
      @Override 
      protected Void doInBackground() throws Exception { 
       TelaDeProcessamento.iniciaTela("Aguarde", "carregando dados"); 
       initiateScreenComponets(); 
       return null; 
      } 


      @Override 
      protected void done() { 
       TelaDeProcessamento.paraTela(); 
       finalizaTela(); 
      } 


     }; 

     worker.execute(); 
    } 

diesen Link ändern half mir https://www.javacodegeeks.com/2012/12/multi-threading-in-java-swing-with-swingworker.html