使用javax.swing.TImer.看一个例子:
编辑:
使用不同的变量,如前所述,计数器变量显示相同的值.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LabelExample {
private Timer timer;
private JButton button;
private JLabel label;
private Color[] labelColors = {
Color.red,
Color.blue
};
private ActionListener timerAction = new ActionListener () {
private int counter1 = 0;
private int counter2 = 1;
@Override
public void actionPerformed ( ActionEvent ae ) {
++counter1;
counter1 %= labelColors.length;
label.setBackground ( labelColors [ counter1 ] );
System.out.println ( "Background Counter: " + counter1 + " Length: " + labelColors.length);
++counter2;
counter2 %= labelColors.length;
label.setForeground ( labelColors [ counter2 ]);
System.out.println ( "Foreground Counter: " + counter2 + " Length: " + labelColors.length);
}
};
public LabelExample () {
}
private void displayGUI () {
JFrame frame = new JFrame ( "Label Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
label = new JLabel ( "Hello World!" );
label.setOpaque ( true );
label.setBackground ( labelColors [ 0 ] );
label.setForeground ( labelColors [ 1 ] );
button = new JButton ( "Stop Timer" );
button.addActionListener ( new ActionListener () {
@Override
public void actionPerformed ( ActionEvent ae ) {
timer.stop ();
}
} );
contentPane.add ( label );
contentPane.add ( button );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
timer = new Timer ( 1000, timerAction );
timer.start ();
}
public static void main ( String[] args ) {
Runnable runnable = new Runnable () {
@Override
public void run () {
new LabelExample ().displayGUI ();
}
};
EventQueue.invokeLater ( runnable );
}
}
编辑2:
关于注释,可以通过以下方式轻松找到更多信息:在您自己的本地计算机上打开SwingUtilities.java文件,移动到Java的安装位置,然后找到src.zip文件夹,以观看任何类的内容.这是SwingUtilities.invokeLater(…)的内容(请阅读注释的第二行):
/**
* Causes doRun.run() to be executed asynchronously on the
* AWT event dispatching thread. This will happen after all
* pending AWT events have been processed. This method should
* be used when an application thread needs to update the GUI.
* In the following example the invokeLater
call queues
* the Runnable
object doHelloWorld
* on the event dispatching thread and
* then prints a message.
*
* Runnable doHelloWorld = new Runnable() {
* public void run() {
* System.out.println("Hello World on " + Thread.currentThread());
* }
* };
*
* SwingUtilities.invokeLater(doHelloWorld);
* System.out.println("This might well be displayed before the other message.");
*
* If invokeLater is called from the event dispatching thread --
* for example, from a JButton's ActionListener -- the doRun.run() will
* still be deferred until all pending events have been processed.
* Note that if the doRun.run() throws an uncaught exception
* the event dispatching thread will unwind (not the current thread).
*
* Additional documentation and examples for this method can be
* found in
* in The Java Tutorial.
*
* As of 1.3 this method is just a cover for java.awt.EventQueue.invokeLater()
.
*
* Unlike the rest of Swing, this method can be invoked from any thread.
*
* @see #invokeAndWait
*/
public static void invokeLater(Runnable doRun) {
EventQueue.invokeLater(doRun);
}