Gói Swing chứa class Timer hỗ trợ cho việc tạo ra các hiệu ứng có tốc độ được quy định trước. Ví dụ, bạn có thể tạo ra một label nhấp nháy theo một tốc độ quy định. Class này có nhiều ứng dụng thực tế.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Time extends JFrame implements ActionListener
{
JLabel l;
Time()
{
Container c = getContentPane();
c.setLayout(null);
l = new JLabel("Blink");
l.setBounds(50, 50, 40, 40);
c.add(l);
Timer t = new Timer(500, this);
// t.setCoalesce(true);
t.start();
}
public void actionPerformed(ActionEvent e)
{
if(l.isVisible())
l.setVisible(false);
else
l.setVisible(true);
}
static void main(String[]arr)throws Exception
{
Time t = new Time ();
t.setSize(200, 200);
t.setVisible(true);
}
}
Trích từ "Thủ thuật lập trình Java"
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Time extends JFrame implements ActionListener
{
JLabel l;
Time()
{
Container c = getContentPane();
c.setLayout(null);
l = new JLabel("Blink");
l.setBounds(50, 50, 40, 40);
c.add(l);
Timer t = new Timer(500, this);
// t.setCoalesce(true);
t.start();
}
public void actionPerformed(ActionEvent e)
{
if(l.isVisible())
l.setVisible(false);
else
l.setVisible(true);
}
static void main(String[]arr)throws Exception
{
Time t = new Time ();
t.setSize(200, 200);
t.setVisible(true);
}
}
Trích từ "Thủ thuật lập trình Java"