提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在方法中创建一个匿名类
import java.awt.event.ActionEvent; /** nowjava.com 提供 **/ import java.awt.event.ActionListener; import javax.swing.JOptionPane; import javax.swing.Timer; public class Main { public static void main(String[] args) { String tickMessage = "Tick..."; String tockMessage = "Tock..."; Timer t = new Timer(1000, new ActionListener() { private boolean tick = true; public void actionPerformed(ActionEvent event) { if (tick) { System.out.println(tickMessage); } else { System.out.println(tockMessage); /*来自 nowjava.com - 时 代 Java*/ } tick = !tick; } }); t.start();