Created
December 23, 2011 15:31
-
-
Save luizfonseca/1514471 to your computer and use it in GitHub Desktop.
Simple Java Clock app for testing the Observer pattern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PACKAGE GOES HERE <--------- | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.GregorianCalendar; | |
import java.util.Observable; | |
import javax.swing.Timer; | |
public class Clock extends Observable implements ActionListener { | |
public static Timer timer; | |
public Clock(){ | |
this.startClock(); | |
} | |
public String getTime(){ | |
String time = ""; | |
GregorianCalendar calendario = new GregorianCalendar(); | |
int h = calendario.get(GregorianCalendar.HOUR_OF_DAY); | |
int m = calendario.get(GregorianCalendar.MINUTE); | |
int s = calendario.get(GregorianCalendar.SECOND); | |
time += ((h < 10) ? "0" : "") + h + ":"; | |
time += ((m < 10) ? "0" : "") + m + ":"; | |
time += ((s < 10) ? "0" : "") + s; | |
return time; | |
} | |
public void actionPerformed(ActionEvent e) { | |
setChanged(); | |
notifyObservers(this.getTime()); | |
} | |
public void startClock() { | |
if (timer == null) { | |
timer = new javax.swing.Timer(1000, this); | |
timer.setInitialDelay(0); | |
timer.start(); | |
} | |
} | |
public void restartClock(){ | |
if (!timer.isRunning()){ | |
timer.restart(); | |
} | |
} | |
public void stopClock() { | |
if (timer != null) { | |
timer.stop(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PACKAGE GOES HERE <------------ | |
import java.awt.FlowLayout; | |
import java.awt.Font; | |
import java.util.Observable; | |
import java.util.Observer; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
public class Form implements Observer { | |
private Clock clock = new Clock(); | |
private JLabel label = new JLabel(); | |
private JButton button = new JButton("Start/Stop"); | |
private static JFrame frame = new JFrame("Clock :D"); | |
public Form(){ | |
this.mountDisplay(); | |
clock.addObserver(this); | |
} | |
public static void main(String[] args){ | |
javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
Form main = new Form(); | |
} | |
}); | |
} | |
public void mountDisplay(){ | |
JPanel panel = new JPanel(); | |
panel.add(label); | |
panel.add(button); | |
panel.setLayout(new FlowLayout(FlowLayout.CENTER)); | |
label.setFont(new Font("Arial", Font.ITALIC, 25)); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.getContentPane().add(panel); | |
frame.setResizable(false); | |
frame.setBounds(200, 200, 500, 110); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
button.addActionListener(new ButtonAction()); | |
label.setText(clock.getTime()); | |
} | |
@Override | |
public void update(Observable arg0, Object arg1) { | |
System.out.println("Hora: " + arg1); | |
label.setText((String)arg1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment