2017-05-11 19 views
0

글쎄, JDialog으로 대기 화면을 만들었지 만 분리되었을 때만 작동합니다. 여기 JDialog에서 GIF가 올바르게 표시되지 않습니다.

내 코드입니다 :

/** 
* 
* @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(); 

    } 

} 

그리고 이것은 결과입니다 : 내가 다른 클래스에서 호출 할 때

enter image description here

,하지만, GIF이 표시되지 않는

JDialog 클래스 예

0 12,

결과 : 내가 잘못

enter image description here

을 뭐하는 거지?

+4

용의자 이것은 AWT 이벤트 발송 스레드 외부에서 작업을 수행하기 때문입니다. 거의 모든 Swing 및 AWT 메소드는 해당 스레드에서만 실행되어야하며 다른 스레드에서는 실행되지 않아야합니다. 이 규칙을 위반하면 이상하고 예측할 수없는 행동으로 이어질 것입니다. https://docs.oracle.com/javase/tutorial/uiswing/concurrency/를 참조하십시오. – VGR

+0

감사합니다 @ VGR SwingWorker의 문제를 해결했습니다 – Krismorte

답변

0

경로에있는 GIF를 찾는 데 문제가있을 수 있습니다 ... 확인하십시오!

코드를 테스트 한 후 코드가 작동해야합니다. enter image description here

+0

경로 문제가 아닙니다 – Krismorte

+0

위의 주석을 읽으십시오, Theading 문제 일 수 있습니다. @Krismorte – Frakcool

+0

나는 둘 다 시도해보고 작동 중입니다 ... IT는 경로와 관련이 있어야합니다 –

0

난 그냥 호출 방법

에게 이전 호출

@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); 
} 

새 통화를 내 클래스를 변경하는 NEET하지 않습니다는

 @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(); 
    } 

이 링크 나에게 https://www.javacodegeeks.com/2012/12/multi-threading-in-java-swing-with-swingworker.html

도움