要求同时2个线程进行,一边显示进度加载条。一边运行后台程序,
当后台程序运行到一定的位置。关闭进度条~
最好有详细的代码。分不够可以再加

解决方案 »

  1.   

    jProgressBar1.setValue(0);
    jProgressBar1.setValue(jProgressBar1.getValue() + 10);
    jProgressBar1.setValue(jProgressBar1.getMaximum());
    JOptionPane.showMessageDialog(this, "完成!");private JProgressBar jProgressBar1 = new JProgressBar();
      

  2.   

    就是现在我要在Frame上弹出一个Dialog,弹出这个Dialog之前我要去数据库读数据,然后把数据画图再显示,这个时间可能比较长。想加个加载条过度下。当我数据库读数据执行完了。就关闭加载条,同时弹出Dialog
      

  3.   

    ProgressMonitorInputStream(Component parentComponent, 
                                  Object message, InputStream in) 
              构造一个对象,以监视输入流的进度。 例如:InputStream in = new BufferedInputStream(
                              new ProgressMonitorInputStream(
                                      parentComponent,
                                      "Reading " + fileName,
                                      new FileInputStream(fileName)));
     这可以创建一个进度监视器,以监视读取输入流的进度
    用这个类试试,
      

  4.   

    进度条和后台程序间可以通过 actionhe
    事件+监听器的机制进行交互
      

  5.   

    /*
    Definitive Guide to Swing for Java 2, Second Edition
    By John Zukowski     
    ISBN: 1-893115-78-X
    Publisher: APress
    */import java.awt.BorderLayout;
    import java.awt.Container;import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;
    import javax.swing.border.Border;public class ProgressSample {
      public static void main(String args[]) {
        JFrame f = new JFrame("JProgressBar Sample");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = f.getContentPane();
        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(25);
        progressBar.setStringPainted(true);
        Border border = BorderFactory.createTitledBorder("Reading...");
        progressBar.setBorder(border);
        content.add(progressBar, BorderLayout.NORTH);
        f.setSize(300, 100);
        f.setVisible(true);
      }
    }
      

  6.   

    自己写的,一个等待窗口
    public class WaitingFrame 
        extends JWindow
        implements WaitingFrameType
    {
        private static WaitingFrame theObj = null;
        private static boolean activity = false;
        private JProgressBar progress = null;
        private JLabel lblShow = null;
        private Frame owner = null;
        
        /**
         * 是否激活
         * @return Returns the activity.
         */
        public static boolean isActivity() {
            return activity;
        }
        
        /**
         * 
         * @return WaitingFrameType
         */
        public static WaitingFrameType getWaitingFrameType(){
            return theObj;
        }    /**
         * 设置是否激活
         * @param activity The activity to set.
         */
        public static void setActivity(boolean activity) {
            WaitingFrame.activity = activity;
        }    /**
         * 构造函数
         * @param owner 
         * @param indeterminate 如果进度条是不确定的为true
         */
        private WaitingFrame(Frame owner, boolean indeterminate){
            super(owner);
            try{
                this.owner = owner;
                this.getContentPane().setLayout(new BorderLayout());
                JPanel pnlMain = new JPanel(new BorderLayout());
                JPanel pnlTop = new JPanel(new BorderLayout());
        
                ImageIcon img = ResourceManager.getImageIcon("logo.gif");
                JLabel label = new JLabel(img);
                label.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
                pnlTop.add(label, BorderLayout.WEST);
               
                lblShow = new JLabel("", JLabel.CENTER);
                pnlTop.add(lblShow, BorderLayout.CENTER);
                
                progress = new JProgressBar();
                pnlTop.add(progress, BorderLayout.SOUTH);
                progress.setStringPainted(true);
                progress.setIndeterminate(indeterminate);
                
                this.getContentPane().add(pnlMain, BorderLayout.CENTER);
                pnlMain.add(pnlTop, BorderLayout.NORTH);
                pnlMain.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
               
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                int width = 500;
                int height = 100;
                int x = (screenSize.width - width)>>1;
                int y = (screenSize.height - height)>>1;
                this.setBounds(x, y, width, height);
           }
           catch(Exception e){
               e.printStackTrace();
           }
        }
        
        /**
         * 显示窗口
         * @param owner
         * @param showText
         * @param indeterminate 如果进度条是不确定的为true
         */
        public static void showIt(Frame owner, String showText, boolean indeterminate){
            if(theObj == null){
                theObj = new WaitingFrame(owner, indeterminate);
            }
            theObj.progress.setString("");
            theObj.lblShow.setText(showText);
            setActivity(true);
            theObj.setVisible(true);
            owner.update(owner.getGraphics());
            theObj.update(theObj.getGraphics());
        }
        
        /**
         * 隐藏窗口
         *
         */
        public static void hideIt(){
            setActivity(false);
            theObj.setVisible(false);
            theObj.owner.update(theObj.owner.getGraphics());
        }
        
        /**
         * 更新进度条上的文字
         * @param str
         */
        public void updateProgress(String str){
            updateProgressText(str);
        }
        
        /**
         * 更新进度条上的文字
         * @param str
         */
        public static void updateProgressText(String str){
            theObj.progress.setString(str);
            theObj.update(theObj.getGraphics());
        }
        
        /**
         * 更新进度条上的文字和刻度
         * @param str
         * @param value
         */
        public static void updateProgressTextAndPos(String str, int value){
            theObj.progress.setString(str);
            theObj.progress.setValue(value);
            theObj.update(theObj.getGraphics());
        }
        
        /**
         * 初始化进度条
         * @param min
         * @param max
         */
        public static void initProgress(int min, int max){
            theObj.progress.setMinimum(min);
            theObj.progress.setMaximum(max);
            theObj.progress.setValue(min);
        }
    }
      

  7.   

    /**
     * 等待窗口类型
     */
    public interface WaitingFrameType {    /**
         * 更新进度条上的文字
         * @param str
         */
        void updateProgress(String str);
    }