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:
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:
Was mache ich falsch?
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
danke @VGR Ich löste das Problem mit SwingWorker – Krismorte