public class Exercise1 extends JFrame {
private JLabel numberLabel = null;
private JButton stopButton = null;
private boolean shouldContinue = true;
private static String[] numbers = new String[] {"1", "7", "15", "3", "9", "0"};
//Mas contenido
}
Después vamos a crear una clase que implemente Runnable para poder convertir el proceso de conteo en un hilo, la clase quedaría como una clase interna más o menos así:
class Counter implements Runnable {
private int index = 0;
public void run() {
while(shouldContinue) {
if(index == numbers.length)
index = 0;
numberLabel.setText("" + numbers[index++]);
try {
Thread.sleep(500);
} catch(InterruptedException ie) { ; }
}
};
}
Agregamos al constructor de la clase un método para arrancar el ciclo, y al botón un método para detenerlo:
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
shouldContinue = false;
}
});
new Thread(new Counter()).start();
La solución estaría completa.
Para seguir...
- Describe el método usado para evitar errores cuando el proceso del contador duerme.
- ¿Qué ventajas y desventajas presenta el hecho de declarar la seríe de números estática?
- ¿Porqué nunca se llama al método run del contador?
- Elabore una lista con las formas de llamar al método deriveFont.
- Describe como podría implementarse la clase del contador dentro del constructor de la solución.
- Describe como podría implementarse la clase ActionListener del botón fuera del constructor.
- Implemente un método para que la lista de valores se llene de manera aleatoria.
Clase completa
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CounterSolution extends JFrame {
private JLabel numberLabel = null;
private JButton stopButton = null;
private boolean shouldContinue = true;
private static String[] numbers = new String[] {"1", "7", "15", "3", "9", "0"};
class Counter implements Runnable {
private int index = 0;
public void run() {
while(shouldContinue) {
if(index == numbers.length)
index = 0;
numberLabel.setText("" + numbers[index++]);
try {
Thread.sleep(500);
} catch(InterruptedException ie) { ; }
}
};
}
//Sobreescribimos el constructor para darle un titulo al frame
public CounterSolution() {
super("Ejercicio 1");
numberLabel = new JLabel(" ", JLabel.CENTER);
stopButton = new JButton("Detener");
//Recuperamos la fuente de la etiqueta
Font font = numberLabel.getFont();
//Recreamos la fuente con un tamaño de 32 puntos
font = font.deriveFont(32.0f);
//Establecemos la fuente nueva de la etiqueta
numberLabel.setFont(font);
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
shouldContinue = false;
}
});
this.setLayout(new BorderLayout());
this.add(numberLabel, BorderLayout.CENTER);
this.add(stopButton, BorderLayout.PAGE_START);
//Ajustamos la ventana para el tamaño de los componentes
this.pack();
//Mostramos la ventana
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(new Counter()).start();
}
public static void main(String[] args) {
new CounterSolution();
}
}
1 comentario:
NO PUES ES LA PRIMERA VES QUE
AGO UN COMENTARIO EN UN FLOG
O PERO BUENO PUES BUENA APORTACION ME SIRVIO MUCHO GRACIIAS!!!
Publicar un comentario