Peguei um exemplo do livro Java como programar 4 edição e ele não roda, não achei o porque (provavelmente copiei errado o sono ta grande) , alguem poderia me ajudar estou usando o Windows2000 talvez possa ser isso.
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.awt.Color;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
public class RandomCharacters extends JApplet implements ActionListener
{
private String strAlfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final int SIZE = 3;
//Vetor com componentes
private JLabel labels[];
private JCheckBox chks[];
private Thread threads[];
private boolean suspended[];
public void init()
{
//define arrays com componentes
labels = new JLabel[SIZE];
chks = new JCheckBox[SIZE];
threads = new Thread[SIZE];
suspended = new boolean[SIZE];
//define layout
getContentPane().setLayout(new GridLayout(SIZE,2,5,5));
//cria componentes, adiciona no painel de conteudo, registra tratador de evento
for (int i = 0; i < SIZE; i++){
labels[i] = new JLabel();
labels[i].setBackground(Color.green);
labels[i].setOpaque(true);
getContentPane().add(labels[i]);
chks[i] = new JCheckBox ( "Suspended");
chks[i].addActionListener(this);
}
}//Fim do método init
public void start()
{
//Criar threads
for (int i = 0 ; i < threads.length ; i++){
//Cria thread atribuindo um objeto da classe que implementa runnable
//Inicia thread
}
}///Dim do método start
private int getIndex(Thread currentThread)
{
//Procura no array de threads a posição da thread que foi passada
for (int i = 0 ; i < threads.length; i++)
//Caso encontre a thread retorna o índice do array onde ela está
if (threads[i] == currentThread)
return i;
//Caso não encontre retornar -1
return -1;
}
public synchronized void stop()
{
for (int i = 0 ; i < threads.length ; i++)
threads[i] = null;
notifyAll();
}
public synchronized void actionPerformed (ActionEvent evento)
{
for (int i=0; i < chks.length; i++){
if (evento.getSource() == chks[i]){
suspended[i] = !suspended[i];
labels[i].setBackground( !suspended[i] ? Color.green : Color.red );
//se a thread for retomada notificar
if (!suspended[i])
notifyAll();
return;
}
}//Fim do laço
}//Fim do método actionPerformed
private class RunnableObj implements Runnable
{
public void run()
{
final Thread CURRENT_THREAD = Thread.currentThread();
final int INDEX = getIndex(CURRENT_THREAD);
while (threads[INDEX] == CURRENT_THREAD){
try {
Thread.sleep((int) (Math.random() * 1000));
//Determina se a thread deve ter sua execução suspensa. Usa
//o applet como monitor
synchronized (RandomCharacters.this){
while(suspended[INDEX] && threads[INDEX] == CURRENT_THREAD){
//Suspende temporariamente a execução da thread
RandomCharacters.this.wait();
}
}
}
catch (InterruptedException erro)
{
System.err.println(erro.toString());
}
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
char chrDisplay = strAlfabeto.charAt((int)(Math.random() * 26));
labels[INDEX].setText(CURRENT_THREAD.getName() + " : " + chrDisplay);
}
});
}
}
}
}
Value