24 April 2016

Progress Bar demo thread runnable

Demo Progress Thread Runnable




package Dmoe;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Progress {

 private JFrame frame;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Progress window = new Progress();
     window.frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the application.
  */
 public Progress() {
  initialize();
 }

 /**
  * Initialize the contents of the frame.
  */
 private JProgressBar progressBar;
 private void initialize() {
  frame = new JFrame();
  frame.setBounds(100, 100, 450, 300);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().setLayout(null);
      
  JButton btnOk = new JButton("ok");
  btnOk.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {
   
    new Thread(new Tempo()).start();
   
   }
  });
  btnOk.setBounds(169, 193, 89, 23);
  frame.getContentPane().add(btnOk);
  
  progressBar = new JProgressBar();
  progressBar.setForeground(Color.GREEN);
  progressBar.setBounds(28, 110, 370, 33);
  frame.getContentPane().add(progressBar);
  
  
 }
 public class Tempo implements Runnable{

  @Override
  public void run() {
   // TODO Auto-generated method stub
   for (int i = 0; i <= 1000; i++) {
    
    progressBar.setValue(i);
    progressBar.repaint();
    try {
     Thread.sleep(50);
    } catch (InterruptedException e) {
     // TODO: handle exception
    }
   }
   
  }
  
 }
}


=>>   progressBar.setStringPainted(true);




0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang