我创建了一个加载窗口来操作数据库。现在我有一个问题,因为当请求时窗口不出现。你能看到我的代码并帮助我吗?
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);
}
}
你正在阻塞事件调度线程,这意味着它不能处理你的请求来显示窗口,直到你停止阻塞它。
考虑使用SwingWorker
,其中你准备和显示窗口,启动工作,并在它的doInBackground
方法中,做你需要做的繁重工作,在done
方法中,关闭窗口并通知任何你可能需要的人,以便继续你的程序执行…
查看Swing中的并发性了解更多细节