Звоните counter.start()
внутри работающего:
// The main method (entry point).
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
counter.start();
}
});
}
Вам действительно нужен определенный порядок вызовов, если вы разместите его вне потока, то счетчик запустится еще до того, как GUI будет существовать, и у вас не получится.
По второму вопросу:
// A method which updates GUI (sets a new value of JLabel).
private static void updateGUI(final int i, final JLabel label) {
SwingUtilities.invokeLater(new Worker(i, label));
}
Вот рабочий:
import javax.swing.JLabel;
public class Worker implements Runnable{
private int i;
private JLabel label;
public Worker(int i, JLabel label) {
this.i = i;
this.label = label;
}
public void run() {
label.setText("You have " + i + " seconds.");
}
}
А твоя главная сейчас:
// The main method (entry point).
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Countdown.showGUI();
counter.start();
}
});
}
UPDATE:
Или, если вы все еще хотите использовать анонимный шаблон, тогда:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Countdown {
static JLabel label;
// Method which defines the appearance of the window.
public static void showGUI() {
JFrame frame = new JFrame("Simple Countdown");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Some Text");
frame.add(label);
frame.pack();
frame.setVisible(true);
}
// Define a new thread in which the countdown is counting down.
public static Thread counter = new Thread() {
public void run() {
for (int i=10; i>0; i=i-1) {
updateGUI(i,label);
try {Thread.sleep(1000);} catch(InterruptedException e) {};
}
}
};
// A method which updates GUI (sets a new value of JLabel).
private static void updateGUI(final int i, final JLabel label) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
label.setText("You have " + i + " seconds.");
}
}
);
}
}