我的程序有3个按钮,每一个按钮都要处理数据库中的不同的数据,我想在处理数据的时候能有提示,比如弹出一个对话框或窗口或其他的东西,上面有文字,说明现在正在进行数据处理,等数据库中的数据处理完后,这个提示就自动消失。我应该怎么作?最好能给出代码。谢谢!

解决方案 »

  1.   

    参考一下:import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class T1113 extends JFrame{
        JButton b = new JButton("execute");
        Processing p;
        public T1113(){
            this.getContentPane().setLayout(new FlowLayout());
            this.getContentPane().add(b);
            b.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   p = new Processing(T1113.this);
                   p.setVisible(true);
                   process();
                   p.setVisible(false);
               }
            });
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(400,300);
            this.setVisible(true);
        }
        
        public void process(){
            try{
                System.out.println("processing");
                Thread.sleep(3000);
                System.out.println("processed");
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }
        }
        
        public static void main(String args[]){
            new T1113();
        }
    }
    class Processing extends JDialog{
        public Processing(JFrame parent){
            super(parent,"processing...",false);
            this.setSize(200,80);
        }
    }
      

  2.   

    zhe 这个要用到多线程。在你处理数据开始启动线程,结束时,终止线程。在线程运行时,加一个提示。
      

  3.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Test extends JFrame implements ActionListener,Runnable
    {
        private JButton btnGood = new JButton("计算");
        private JLabel jLabel=new JLabel("");
        private Thread thread;
        private JPanel contentPane;
    private JPanel panel = new JPanel();    public Test()
        {
            thread=new Thread(this);
            contentPane = (JPanel)this.getContentPane();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(btnGood);
            panel.add(jLabel);
            btnGood.addActionListener(this);
            contentPane.add(panel);
            this.setSize(270,250);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
    this.setVisible(true);
        }    public void actionPerformed(ActionEvent evt)
    {
    if (evt.getSource() == btnGood)
    {
    thread.start();
    for(int i=0;i<1000;i++)
       for(int j=0;j<1000;j++)
       {} try{
    thread.sleep(100);
    }catch(InterruptedException e){}
    thread=null;
    }
    }    public void run()
        {
            while(thread!=null)
            {
    if(jLabel.getText()==""||jLabel.getText().equals(""))  jLabel.setText("系统正在进行信息处理,请等待!");
    }
    jLabel.setText("");
        }    public static void main(String args[]){
            new Test();
        }
    }z这个程序有问题,谁改一下?
      

  4.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Test2 extends JFrame implements ActionListener
    {
        private JButton btnGood = new JButton("计算");
        private JLabel jLabel=new JLabel("系统正在进行信息处理,请等待!");
        private JPanel contentPane;
    private JPanel panel = new JPanel();    public Test2()
        {
            contentPane = (JPanel)this.getContentPane();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(btnGood);
            panel.add(jLabel);
            jLabel.setVisible(false);
            btnGood.addActionListener(this);
            contentPane.add(panel);
            this.setSize(270,250);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
    this.setVisible(true);
        }    public void actionPerformed(ActionEvent evt)
    {
    if (evt.getSource() == btnGood)
    {
    jLabel.setVisible(true);
    System.out.println("start");
    try{
    Thread.sleep(3000);
    }catch(InterruptedException ie){
                ie.printStackTrace();
            }
    jLabel.updateUI(); for(int i=0;i<100000;i++)
       for(int j=0;j<100000;j++)
       {}
    System.out.println("end");
    jLabel.setVisible(false);
    }
    }    public static void main(String args[]){
            new Test2();
        }
    }这个也不行,swing控件好像反应迟钝
      

  5.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class MainClass  extends JFrame implements  ActionListener,Runnable
    {
        boolean flag = false;
        HintJDialog hintJDialog=new HintJDialog(this);
        static Thread t;
        static Thread t2;
        private JButton btnGood = new JButton("计算");
    private JPanel contentPane;
    private JPanel panel = new JPanel();    public MainClass()
        {
    //t = new Thread(this, "run");
            //t2 = new Thread(this, "wait");
    contentPane = (JPanel)this.getContentPane();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
    panel.add(btnGood);
    btnGood.addActionListener(this);
    contentPane.add(panel);
    this.setSize(270,250);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
    this.setVisible(true);
    }    public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            MainClass m = new MainClass();    }    public void run()
        {            if (Thread.currentThread().getName().startsWith("r"))
                {
                    calculate();
                }
                else
                {
                    waiting();
                }
        }    public void actionPerformed(ActionEvent evt)
    {
    if (evt.getSource() == btnGood)
    {
    t = new Thread(this, "run");
             t2 = new Thread(this, "wait");
    //if(!t.isAlive()) t.start();
             //if(!t2.isAlive()) t2.start();
             t.start();
             t2.start();
    }
    }    protected synchronized void calculate()
        {
            int i = 0;
            try
            {
                hintJDialog.setVisible(true);
                while (i < 100)
                {
                    Thread.sleep(30);
                    System.out.println("计算到" + i);
                    i++;
                }
                flag = true;
                this.notifyAll();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        protected synchronized void waiting()
        {
            try
            {
                while (!flag)
                {
                    wait();
                }
                hintJDialog.setVisible(false);
                System.out.println("执行完毕......");
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
      

  6.   

    import javax.swing.*;
    import java.awt.*;public class HintJDialog extends JDialog 
    {
    private ImageIcon imageIcon1=new  ImageIcon("gif\\1.gif");
    private ImageIcon imageIcon2=new  ImageIcon("gif\\2.gif");
    private ImageIcon imageIcon3=new  ImageIcon("gif\\3.gif");
    private ImageIcon imageIcon4=new  ImageIcon("gif\\4.gif");
    private JLabel jLabel;

    public HintJDialog(JFrame parent) 
    {
    super(parent,"系统正在进行信息处理,请等待!",false);
    this.getContentPane().setLayout(new GridLayout(2,1,0,0));
    jLabel=new JLabel(imageIcon1);
    this.getContentPane().add(jLabel);
    jLabel=new JLabel(imageIcon2);
    this.getContentPane().add(jLabel);
    jLabel=new JLabel(imageIcon3);
    this.getContentPane().add(jLabel);
    jLabel=new JLabel(imageIcon4);
    this.getContentPane().add(jLabel);
    this.setSize(270,250);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
    }
    }