`
czwangelo
  • 浏览: 71060 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

进度条的使用

阅读更多
每隔一秒钟,进度条的使用
java 代码
  1. package example;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Color;  
  5. import java.awt.Container;  
  6. import java.awt.GridLayout;  
  7. import java.awt.event.ActionEvent;  
  8. import java.awt.event.ActionListener;  
  9. import java.awt.event.MouseAdapter;  
  10. import java.awt.event.MouseMotionAdapter;  
  11.   
  12. import javax.swing.ImageIcon;  
  13. import javax.swing.JButton;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JLabel;  
  16. import javax.swing.JOptionPane;  
  17. import javax.swing.JPanel;  
  18. import javax.swing.JProgressBar;  
  19. import javax.swing.Timer;  
  20.   
  21. public class GlassExample extends JFrame {  
  22.   JPanel glass = new JPanel(new GridLayout(01));  
  23.   JProgressBar waiter = new JProgressBar(0100);  
  24.   Timer timer;  
  25.   
  26.   public GlassExample( ) {  
  27.     super("GlassPane Demo");  
  28.     setSize(500300);  
  29.     setDefaultCloseOperation(EXIT_ON_CLOSE);  
  30.   
  31.     // Set up the glass pane with a little message and a progress bar.  
  32.     JPanel controlPane = new JPanel(new GridLayout(2,1));  
  33.     controlPane.setOpaque(false);  
  34.     controlPane.add(new JLabel("Please wait..."));  
  35.     controlPane.add(waiter);  
  36.     glass.setOpaque(false);  
  37.     glass.add(new JLabel( )); // Padding...  
  38.     glass.add(new JLabel( ));  
  39.     glass.add(controlPane);  
  40.     glass.add(new JLabel( ));  
  41.     glass.add(new JLabel( ));  
  42.     glass.addMouseListener(new MouseAdapter( ) {});  
  43.     glass.addMouseMotionListener(new MouseMotionAdapter( ) {});  
  44.     setGlassPane(glass);  
  45.   
  46.     // Now set up a few buttons and images for the main application.  
  47.     JPanel mainPane = new JPanel( );  
  48.     mainPane.setBackground(Color.white);  
  49.     JButton redB = new JButton("Red");  
  50.     JButton blueB = new JButton("Blue");  
  51.     JButton greenB = new JButton("Green");  
  52.     mainPane.add(redB);  
  53.     mainPane.add(greenB);  
  54.     mainPane.add(blueB);  
  55.     mainPane.add(new JLabel(new ImageIcon("oreilly.gif")));  
  56.   
  57.     // Attach the pop-up debugger to the main app buttons so you  
  58.     // see the effect of making a glass pane visible.  
  59.     PopupDebugger pd = new PopupDebugger(this);  
  60.     redB.addActionListener(pd);  
  61.     greenB.addActionListener(pd);  
  62.     blueB.addActionListener(pd);  
  63.   
  64.     // And last but not least, our button to launch the glass pane  
  65.     JButton startB = new JButton("Start the big operation!");  
  66.     startB.addActionListener(new ActionListener( ) {  
  67.         public void actionPerformed(java.awt.event.ActionEvent A) {  
  68.           glass.setVisible(true);  
  69.           startTimer( );  
  70.         }  
  71.       });  
  72.   
  73.     Container contentPane = getContentPane( );  
  74.     contentPane.add(mainPane, BorderLayout.CENTER);  
  75.     contentPane.add(startB, BorderLayout.SOUTH);  
  76.   }  
  77.   
  78.   // A quick method to start up a 10-second timer and update the progress bar  
  79.   public void startTimer( ) {  
  80.     if (timer == null) {  
  81.       timer = new Timer(1000new ActionListener( ) {  
  82.           int progress = 0;  
  83.           public void actionPerformed(ActionEvent A) {  
  84.             progress += 1;  
  85.             waiter.setValue(progress);  
  86.   
  87.             // Once we hit 100%, remove the glass pane and reset the progress bar  
  88.             // stuff.  
  89.             if (progress >= 100) {  
  90.               progress = 0;  
  91.               timer.stop( );  
  92.               glass.setVisible(false);  
  93.               waiter.setValue(0);  
  94.             }  
  95.           }  
  96.         });  
  97.     }  
  98.     if (timer.isRunning( )) {  
  99.       timer.stop( );  
  100.     }  
  101.     timer.start( );  
  102.   }  
  103.   
  104.   // A graphical debugger that pops up whenever a button is pressed  
  105.   public class PopupDebugger implements ActionListener {  
  106.     private JFrame parent;  
  107.     public PopupDebugger(JFrame f) {  
  108.       parent = f;  
  109.     }  
  110.     public void actionPerformed(ActionEvent ae) {  
  111.       JOptionPane.showMessageDialog(parent, ae.getActionCommand( ));  
  112.     }  
  113.   }  
  114.   
  115.   public static void main(String[] args) {  
  116.     GlassExample ge = new GlassExample( );  
  117.     ge.setVisible(true);  
  118.   }  
  119. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics