⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progressbardemo1.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// ProgressBarDemo1.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class ProgressBarDemo1 extends JFrame
{
   JProgressBar pbar;
   ProgressThread pthread;

   ProgressBarDemo1 (String title)
   {
      super (title);

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      pbar = new JProgressBar ();
      getContentPane ().add (pbar, BorderLayout.NORTH);

      JPanel jp = new JPanel ();

      ActionListener l = new ActionListener ()
                         {
                             public void actionPerformed (ActionEvent e)
                             {
                                if (pthread == null ||
                                    !pthread.isAlive ())
                                {
                                    pthread = new ProgressThread (pbar);
                                    pthread.start ();
                                }
                             }
                         };

      JButton jb = new JButton ("Start");
      jb.addActionListener (l);
      jp.add (jb);

      l = new ActionListener ()
          {
              public void actionPerformed (ActionEvent e)
              {
                 if (pthread != null)
                     pthread.setStop (true);
              }
          };

      jb = new JButton ("Stop");
      jb.addActionListener (l);
      jp.add (jb);

      getContentPane ().add (jp, BorderLayout.SOUTH);

      setSize (200, 100);
      setVisible (true);
   }

   public static void main (String [] args)
   {
      new ProgressBarDemo1 ("ProgressBar Demo1");
   }
}

class ProgressThread extends Thread
{
   JProgressBar pbar;
   boolean stopped;

   ProgressThread (JProgressBar pbar)
   {
      this.pbar = pbar;
   }

   void setStop (boolean state)
   {
      stopped = state;
   }

   public void run ()
   {
      int min = 0;
      int max = 50;

      pbar.setMinimum (min);
      pbar.setMaximum (max);
      pbar.setValue (min);

      for (int i = min; i <= max; i++)
           if (stopped)
               break;
           else
           {
               pbar.setValue (i);
               try
               {
                  Thread.sleep (100);
               }
               catch (InterruptedException e) {}
           }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -