例えば。-クラスを受講する
import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;
public class flashThread implements Runnable {
private JLabel temp;
Thread thread;
Color randColor;
public flashThread(JLabel toFlash) {
temp = toFlash;
thread = new Thread(this);
}
public void run() {
Random r = new Random();
while (true) {
temp.setForeground(new Color(r.nextInt(246) + 10,
r.nextInt(246) + 10, r.nextInt(246) + 10));
String tempString = temp.getText();
temp.setText("");
try {
thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
}
temp.setText(tempString);
try {
thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
}
}
}
public void begin() {
thread.start();
}
}
コンストラクターにthread.start()を追加した場合、flashThreadのオブジェクトを2つ作成すると、そのうちの1つだけが点滅します。ただし、削除した場合は、begin()メソッドを追加してから、両方のflashThreadオブジェクトを初期化したクラスからbeginメソッドを呼び出すと、両方がフラッシュします。
ヘルプ-スレッドについて学び始めたばかりです。
前もって感謝します!-自分