0

我正在尝试显示加载动画(一个 gif 文件),同时使用 java 连接并从 gmail imap 服务器获取所有消息,但动画不可见......我的代码如下所示,如果有的话,请帮助我解决方案存在....

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.mail.*;
import java.awt.event.*;
import javax.mail.*;
import javax.mail.event.*;
import java.util.*;



public class vmail extends JFrame implements ActionListener,MouseMotionListener
{
    Container cp;
    JLabel background,loader;
    ImageIcon ic1,ic2;
    JTextField tuname,tpassword;
    JButton blogin;
    JPanel jp_login;

    public vmail()
    {
        setSize(900,640);
        setTitle("GMAIL CLIENT ");
        setLocation(200,50);

        cp=getContentPane();
        cp.setLayout(null);
        cp.setBackground(Color.white);


        String workingDir = System.getProperty("user.dir");
        workingDir=workingDir.substring(0,14);

        loader=new JLabel("");
        ic2=new ImageIcon(workingDir+"\\images\\progressbar1.gif");
        loader.setIcon(ic2);
        loader.setBounds(230,90,400,400);
        loader.setVisible(false);
        cp.add(loader); 



        jp_login=new JPanel();
        jp_login.setLayout(null);








        tuname=new JTextField(10);
        jp_login.add(tuname);
        tuname.setBounds(604,163,245,30);
        tuname.addMouseMotionListener(this);


        tpassword=new JTextField(10);
        jp_login.add(tpassword);
        tpassword.setBounds(604,224,245,30);
        tpassword.addMouseMotionListener(this);



        blogin=new JButton("");

        blogin.setBounds(605,271,53,29);
        blogin.setOpaque(false);
        blogin.setContentAreaFilled(false);
        blogin.setBorderPainted(false);
        blogin.addActionListener(this);
        blogin.addMouseMotionListener(this);
        jp_login.add(blogin);



        background=new JLabel("");

        System.out.println(workingDir);
        ic1=new ImageIcon(workingDir+"\\images\\background.png");
        background.setIcon(ic1);
        background.setBounds(0,0,900,600);

        jp_login.add(background);




        jp_login.setBounds(0,0,900,600);
        cp.add(jp_login);


        setVisible(true);
        setDefaultCloseOperation(3);
    }

    public static void main(String[] args) 
    {
        UIManager.LookAndFeelInfo info[];
        info=UIManager.getInstalledLookAndFeels();
        String name=info[3].getClassName();

        try
        {
            UIManager.setLookAndFeel(name);
        }  
        catch(Exception e)
        {
            System.out.println(e);
        }

        vmail v=new vmail();

    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==blogin)
        {




            if(tuname.getText().equals("")||tpassword.getText().equals(""))
            {
                JOptionPane.showMessageDialog(this,"Incorrect Data Provided");
            }
            else
            {
                loader.setVisible(true);


                Properties props = System.getProperties();
                props.setProperty("mail.store.protocol", "imaps");
                try 
                {

                        Session session = Session.getDefaultInstance(props, null);
                        Store store = session.getStore("imaps");
                          try
                          {
                            store.connect("imap.gmail.com", tuname.getText(),tpassword.getText());
                            System.out.println("The Store connected is :  "+store);
                          }
                          catch(Exception e)
                          {
                            loader.setVisible(false);
                            JOptionPane.showMessageDialog(this,"Invalid User..");
                            System.out.println("Cannot connect error \n\n"+e);
                          }

                        vmail_in v=new vmail_in(store);
                        loader.setVisible(false);
                        this.setVisible(false);



                }
                catch(Exception e)
                {
                        loader.setVisible(false);
                    System.out.println("Setup Connection Error \n\n"+e);
                }
                loader.setVisible(false);   

            }
        }
    }


    public void mouseMoved(MouseEvent e) 
    {
                    if(e.getSource()==tuname)
                      tuname.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    if(e.getSource()==tpassword)
                      tpassword.setCursor(new Cursor(Cursor.HAND_CURSOR)); 
                    if(e.getSource()==blogin)
                     blogin.setCursor(new Cursor(Cursor.HAND_CURSOR));      

    }

    public void mouseDragged(MouseEvent e) 
        {  }





}
4

1 回答 1

2

那是因为您试图在事件调度线程(又名 EDT)的上下文中建立与 IMAP 存储的连接。

EDT 的职责之一是发送绘制请求,因此当您发送它时,它无法响应任何重绘更新(或用户输入)。

在启动连接动画后,您需要使用某种后台线程来执行连接过程。

我建议你看一下Concurrency in Swing并特别注意该SwingWorker部分

于 2013-01-31T08:54:34.557 回答