Skip to content

Instantly share code, notes, and snippets.

@thetekst
Created May 23, 2012 19:32
Show Gist options
  • Save thetekst/2777267 to your computer and use it in GitHub Desktop.
Save thetekst/2777267 to your computer and use it in GitHub Desktop.
InnerClassTest
//InnerClassTest.java
import javax.swing.*;
public class InnerClassTest
{
public static void main(String[] args)
{
TalkingClock clock = new TalkingClock(1000, true);
// TalkingClock.setBeep(false);
clock.start();
// keep program running until user selects "Ok"
JOptionPane.showMessageDialog(null, "Quit program?");
System.exit(0);
}
}
//TalkingClock.java
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class TalkingClock
{
public TalkingClock(int interval, boolean beep)
{
this.interval = interval;
TalkingClock.setBeep(beep);
}
public void start()
{
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
public static boolean isBeep() {
return beep;
}
public static void setBeep(boolean beep) {
TalkingClock.beep = beep;
}
private int interval;
private static boolean beep;
}
//TimePrinter.java
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
public class TimePrinter implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Date now = new Date();
System.out.println("The time is " + now);
if (TalkingClock.isBeep()) Toolkit.getDefaultToolkit().beep();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment