我为具有数据库的操作创建了一个加载窗口。现在我遇到了问题,因为请求时窗口不会出现。 你能看到我的代码并帮助我吗?
private void btCadastrarUsuarioActionPerformed(java.awt.event.ActionEvent evt) {
LoadingWindow lw = new LoadingWindow(this);
lw.show(null,jpanel1);
if(confereSobrenome==true && confereEmail==true && confereSenha==true &&
confereNome==true && confereUser==true && confereSenhaConfirmacao==true){
FileInputStream fis;
if (fileFoto==null){
String path = this.getClass().getResource("/img/no_pic.jpg").getFile();
fileFoto=new File(path);
}
ImageIcon icone;
try {
Image image = ImageIO.read(fileFoto);
icone = new ImageIcon(image);
} catch (IOException ex) {
icone = new ImageIcon(getClass().getResource("/img/no_pic.jpg"));
}
icone.setDescription(fileFoto.getAbsolutePath());
UsuarioDao dao = new UsuarioDao();
if(btCadastrarUsuario.getText().equals("Atualizar")){
user.setFoto(icone);
dao.alteraUsuario(user);
}else{
Usuario user = new Usuario(txtUser.getText(), icone, txtPrimeiroNome.getText(), txtUltimoNome.getText(), new String (txtSenha.getPassword()), txtEmail.getText(),0,0);
dao.adicionaUsuario(user);
}
if(btCadastrarUsuario.getText().equals("Atualizar")){
btCadastrarUsuario.setText("Cadastrar");
CardLayout cards = (CardLayout)(jpPrincipal.getLayout());
cards.show(jpPrincipal, "apostas");
jLabel4.setIcon(new Util().dimencionaImagem(user.getFoto(),80));
}else{
this.dispose();
BolaoFuleco.main(null);
newUser=false;
CardLayout cards = (CardLayout)(jpPrincipal.getLayout());
cards.show(jpPrincipal, "cadastro");
}
}
try {
Thread.sleep(1300);
} catch (InterruptedException ex) {
Logger.getLogger(bolaoJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
lw.close();
}
这里我们有LoadingWindow类:
public class LoadingWindow extends JWindow{
public LoadingWindow(Frame owner) {
super(owner);
inicialize();
}
public void inicialize(){
setLayout(null);
//Util.centralizarJanela(this, 150, 150);
getContentPane().setBackground(Color.BLACK);
getContentPane().setLayout(new GridLayout(1,1));
JLabel imgLoading = new JLabel(new javax.swing.ImageIcon(getClass().getResource("/img/ipad_loading_40x40.gif")));
getContentPane().add(imgLoading);
//70% de transparência
AWTUtilities.setWindowOpacity(this, .8f);
setSize(50,50);
}
public void show(String texto, java.awt.Component componentOwner){
setLocationRelativeTo(componentOwner);
if(texto!=null){
getContentPane().setLayout(new GridLayout(2,1));
JLabel lbTexto = new JLabel(texto);
lbTexto.setForeground(Color.WHITE);
lbTexto.setFont(new java.awt.Font("Arial", 1, 12));
lbTexto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
getContentPane().add(lbTexto);
}
setVisible(true);
}
public void close(){
setVisible(false);
dispose();
}
public void noShow(){
setVisible(false);
}
}
答案 0 :(得分:0)
您正在阻止事件调度线程,这意味着在您停止阻止它之前,它无法处理您的请求以显示窗口。
考虑使用SwingWorker
,在其中准备并显示窗口,启动工作人员,并使用doInBackground
方法,执行您需要做的繁重工作,并在done
中方法,关闭窗口并通知您可能需要的任何其他人,以便继续执行程序......
请查看Concurrency in Swing了解详情