GIF在JDialog上无法正确显示

时间:2017-05-11 16:13:30

标签: java image swing gif event-dispatch-thread

好吧,我开发了一个带有JDialog的等待屏幕,但它只在隔离时才有效。

这是我的代码:

/**
 *
 * @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班级示例

调用
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);

结果:

enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可能在路径中找到gif时遇到问题...验证!!

尝试完代码后,我可以确认您的代码必须正常工作! enter image description here

答案 1 :(得分: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);
}

新来电

       @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