Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/01/2012, 14:56
Avatar de pcarvajal
pcarvajal
 
Fecha de Ingreso: enero-2002
Mensajes: 701
Antigüedad: 22 años, 4 meses
Puntos: 8
Respuesta: Hilos con Swing

Este es el código que estoy utilizando:

Main.java
Código PHP:
package catedra;

import java.awt.Color;
import javax.swing.JOptionPane;

public class 
Main extends javax.swing.JFrame implements Runnable {

    
/** Creates new form Main */
    
public Main() {
        
initComponents();
        
btnIniciar.setEnabled(false);
        
btnPausa.setEnabled(false);
        
btnDetener.setEnabled(false);
        
btnIniciar2.setEnabled(false);
        
btnPausa2.setEnabled(false);
        
btnDetener2.setEnabled(false);
    }

    
/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
@SuppressWarnings("unchecked")
    
// <editor-fold defaultstate="collapsed" desc="Generated Code">
    
private void initComponents() {
....
}
    private 
void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {                                           
      try{
        
Hilo1.iniciarHilo(jTextField1);
        
btnIniciar.setEnabled(false);
        
btnPausa.setEnabled(true);
        
btnDetener.setEnabled(true);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al iniciar el hilo");
      }
    }                                          

    private 
void btnDetenerActionPerformed(java.awt.event.ActionEvent evt) {
      try{
        
Hilo1.detenerHilo(jTextField1);
        
jTextField1.setBackground(Color.LIGHT_GRAY);
        
btnNuevo.setEnabled(true);
        
btnPausa.setEnabled(false);
        
btnDetener.setEnabled(false);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al detener el hilo");
      }
    }

    private 
void btnPausaActionPerformed(java.awt.event.ActionEvent evt) {
      try{
        
Hilo1.pausarHilo(jTextField1);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al pausar el hilo");
      }
    }

    private 
void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {
        
/*
        JOptionPane.showMessageDialog(null, "DISEÑADO POR :\n\n"
        + "ELMER JOSE DIAZ LASSO\n\n"
        + "ESTUDIANTE 5° TS UNIAJC\n\n"
        + "SANTIAGO DE CALI\n\n"
        + "VALLE");     
         */
        //JOptionPane.showMessageDialog(null,Hilo1..interrupted());
        
Hilo1.detenerHilo(jTextField1);
        
Hilo2.detenerHilo(jTextField2);
        
System.exit(0);
    }

    private 
void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {
      try{  
        
btnNuevo.setEnabled(false);
        
jTextField1.setBackground(Color.white);
        
jTextField1.setText("0");
        
btnIniciar.setEnabled(true);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al crear el hilo");
      }
    }

    
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
private void btnNuevo2ActionPerformed(java.awt.event.ActionEvent evt) {
      try{  
        
btnNuevo2.setEnabled(false);
        
jTextField2.setBackground(Color.white);
        
jTextField2.setText("0");
        
btnIniciar2.setEnabled(true);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al crear el hilo");
      }
    }

    private 
void btnIniciar2ActionPerformed(java.awt.event.ActionEvent evt) {
      try{
        
Hilo2.iniciarHilo(jTextField2);
        
btnIniciar2.setEnabled(false);
        
btnPausa2.setEnabled(true);
        
btnDetener2.setEnabled(true);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al iniciar el hilo");
      }
    }

    private 
void btnPausa2ActionPerformed(java.awt.event.ActionEvent evt) {
      try{
        
Hilo2.pausarHilo2(jTextField2);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al pausar el hilo");
      }
    }

    private 
void btnDetener2ActionPerformed(java.awt.event.ActionEvent evt) {
      try{
        
Hilo2.detenerHilo(jTextField2);
        
jTextField2.setBackground(Color.LIGHT_GRAY);
        
btnNuevo2.setEnabled(true);
        
btnPausa2.setEnabled(false);
        
btnDetener2.setEnabled(false);
      }
      catch (
Exception e){
        
JOptionPane.showMessageDialog(null,"Se produjo un error al detener el hilo");
      }
    }

    
/**
    * @param args the command line arguments
    */
    
public static void main(String args[]) {
        
java.awt.EventQueue.invokeLater(new Runnable() {
            public 
void run() {
                new 
Main().setVisible(true);
                
            }
        });
    }

    
// Variables declaration - do not modify
    
private javax.swing.JButton btnDetener;
    private 
javax.swing.JButton btnDetener2;
    private 
javax.swing.JButton btnIniciar;
    private 
javax.swing.JButton btnIniciar2;
    private 
javax.swing.JButton btnNuevo;
    private 
javax.swing.JButton btnNuevo2;
    private 
javax.swing.JButton btnPausa;
    private 
javax.swing.JButton btnPausa2;
    private 
javax.swing.JButton btnSalir;
    private 
javax.swing.JPanel jPanel1;
    private 
javax.swing.JTextField jTextField1;
    private 
javax.swing.JTextField jTextField2;
    
// End of variables declaration

    
public void run() {
        throw new 
UnsupportedOperationException("Not supported yet.");
    }