Ver Mensaje Individual
  #5 (permalink)  
Antiguo 19/03/2009, 23:04
hughMiEmporio
 
Fecha de Ingreso: marzo-2009
Mensajes: 1
Antigüedad: 15 años
Puntos: 0
Respuesta: Barra de progreso

Aqui os dejo un codigo que hice de una barra de progreso espero que a alguien le sirva como base para que puedan adaptarla o darse una idea para poderla implementar de acuerdo al programa que necesiten


import java.awt.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JProgressBar;


/**
*
* @author MI EMPORIO
*/
class Vamos implements ActionListener
{
JButton iniciar;
JButton parar;
JProgressBar barra;
JFrame frame;
Timer tiempo;
int segundos;
boolean bandera;
public Vamos()
{
frame = new JFrame("Barra de Progreso");
barra = new JProgressBar();
barra.setStringPainted(true);
iniciar = new JButton("Iniciar");
parar = new JButton("Parar");
frame.add(barra).setBounds(10,10,250,20);
frame.add(iniciar).setBounds(10,50,100,20);
frame.add(parar).setBounds(150,50,100,20);
bandera=true;
segundos=0;
frame.setSize(400,300);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

iniciar.addActionListener(this);
parar.addActionListener(this);



}


public void actionPerformed(ActionEvent e) {
if(e.getSource()==iniciar)
{
bandera=false;
tiempo = new Timer();
tiempo.schedule(new Vamos1(),0,1*100);

}else if(e.getSource()==parar)
{
tiempo.cancel();
int b=JOptionPane.showConfirmDialog(frame,"","",JOptio nPane.YES_NO_CANCEL_OPTION);
if(b==0)
bandera=true;
if(b==1)
bandera=false;
tiempo = new Timer();
tiempo.schedule(new Vamos1(),0,1*100);

if(b==2)
bandera=false;




}
}



class Vamos1 extends TimerTask
{
public void run()
{
segundos++;

barra.setValue(segundos);

if(bandera&&segundos<=100)
{

JOptionPane.showMessageDialog(frame,"PROCESO CANCELADO","",JOptionPane.YES_NO_OPTION);
parar.setEnabled(false);
iniciar.setEnabled(false);

}else if (bandera&&segundos>100)
{
tiempo.cancel();
System.out.println("PROCESO TERMINADO");
}


}
}

}
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame.setDefaultLookAndFeelDecorated(true);
Vamos y = new Vamos ();


}

}