*** @author M. Esponda* @version 1.0*/import java.awt.*;import java.awt.event.*;public class ClockFrame extends Frame { public void main( String[] args ) { ClockFrame cf = new ClockFrame(); Timer clock = new Timer(cf); clock.start(); } // Time-Objekt wo die aktuelle Zeit des Uhr gespeichert wird; Time time; // Hier wird die Zeit ausgegeben private Label timeLabel; // Hier kann man im Clock-Fenster text ausgeben. public static TextArea text; private Button setTimeButton; private TextArea hoursText; private TextArea minsText; private TextArea secsText; private Button stopTime; private Button forward; private Button rewind; private int step; private Panel buttonsPanel; // Konstruktor public ClockFrame() { time = new Time(); text = new TextArea(""); this.setLayout( new GridLayout(3,1,8,8)); this.setBackground( new Color( 00,180,250 )); this.setSize(350,300); this.add(text); this.initTimePanel(); this.initButtonsPanel(); this.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e) { System.out.println("Exiting..."); ClockFrame.this.dispose(); System.exit(0); } // end of method windowClosing } // end of anonym class ); // end of add setVisible(true); } // end of Konstruktor void initTimePanel() { timeLabel = new Label( "12:00:00" ); timeLabel.setFont( new Font( "verdana", 4, 36 ) ); timeLabel.setForeground(Color.white); Panel timePanel = new Panel(); timePanel.setBackground( new Color( 50,100,220 ) ); timePanel.add(timeLabel); this.add(timePanel); } void initButtonsPanel() { setTimeButton = new Button("set"); hoursText = new TextArea("",1,1,TextArea.SCROLLBARS_NONE); minsText = new TextArea("",1,1,TextArea.SCROLLBARS_NONE); secsText = new TextArea("",1,1,TextArea.SCROLLBARS_NONE); stopTime = new Button(" stop "); forward = new Button(" forward "); rewind = new Button(" rewind "); buttonsPanel = new Panel(); buttonsPanel.setLayout( new FlowLayout() ); StopButtonActionListener sl = new StopButtonActionListener(); stopTime.addActionListener(sl); stopTime.setBackground(Color.green); ForwardButtonActionListener fl = new ForwardButtonActionListener(); forward.addActionListener(fl); forward.setBackground(Color.green); RewindButtonActionListener rl = new RewindButtonActionListener(); rewind.addActionListener(rl); rewind.setBackground(Color.green); SetButtonActionListener sbl = new SetButtonActionListener(); setTimeButton.addActionListener(sbl); setTimeButton.setBackground(Color.green); buttonsPanel.add(stopTime); buttonsPanel.add(forward); buttonsPanel.add(rewind); buttonsPanel.add(setTimeButton); buttonsPanel.add(hoursText); buttonsPanel.add(minsText); buttonsPanel.add(secsText); buttonsPanel.setBackground(new Color( 00,180,250 )); this.add(buttonsPanel); } public int getStep() { return step; } public Label getTimeLabel() { return timeLabel; } class StopButtonActionListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { step = 0; } // end of actionPerformed } // end of class StopButtonActionListener class ForwardButtonActionListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { step = 1; } // end of actionPerformed } // end of class ForwardButtonActionListener class RewindButtonActionListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { step = -1; } // end of actionPerformed } // end of class RewindButtonActionListener class SetButtonActionListener implements ActionListener { public void actionPerformed( ActionEvent ae ) { try { time.setTime( Integer.parseInt(hoursText.getText()), Integer.parseInt(minsText.getText()), Integer.parseInt(secsText.getText()) ); } catch( NumberFormatException nfe ) { ClockFrame.write( "Falsche oder unvollständige Angaben!\n " +"versuche es nochmal!" ); } } // end of actionPerformed } // end of SetButtonActionListener public static void write( String text ) { ClockFrame.text.append(text); } // Konstruktor class Time extends ClockFrame { // Instanzvariablen // private int seconds; // private int minutes; // private int hours; int seconds = 0; int minutes = 0; int hours = 0; //} // Methoden public void nextHour() { hours = hours + 1; if (hours > 23) { hours = 0; } } public void nextMinute() { minutes = minutes + 1; if (minutes > 59) { minutes = 0; nextHour(); } } public void nextSecond() { seconds = seconds + 1; if (seconds > 59) { seconds = 0; nextMinute(); } } public void previousHour() { hours = hours - 1; if (hours < 0) { hours = 0; } } public void previousMinute() { minutes = minutes - 1; if (minutes < 0) { minutes = 59; previousHour(); } } public void previousSecond() { seconds = seconds - 1; if (seconds < 0) { seconds = 59; previousMinute(); } } public void setTime( int stunden, int minuten, int sekunden ) { if ( (sekunden > 59) || (sekunden < 0) || (minuten > 59) || (minuten < 0) || (stunden > 23) || (stunden < 0) ) { // ClockFrame.write("Time parameter outside of expected range: "+ // stunden+","+minuten+","+sekunden); } else { hours = stunden; minutes = minuten; seconds = sekunden; } } public String toString() { return twoDigits(hours)+":"+twoDigits(minutes)+":"+twoDigits(seconds); } private String twoDigits( int number ) { if ( number<10 ) return "0"+number ; else return ""+number ; }} // end of class ClockFrame/*** @author M.Esponda* @version 1.0*/public class Timer extends Thread { public final static int ONE_SECOND = 1000; ClockFrame clockframe; public Timer( ClockFrame cf ) { clockframe = cf; } public void run() { long m = (System.currentTimeMillis()/1000)%86400; Time ct = clockframe.time; ct.setTime((int) m/3600, (int)(m/60)%60, (int)(m)%60 ); ct.nextHour(); ct.nextHour(); while ( true ) { try { this.sleep(ONE_SECOND); } catch ( InterruptedException ie ) {} switch (clockframe.getStep()) { case 0: break; case -1: ct.previousSecond(); break; case 1: ct.nextSecond(); } clockframe.getTimeLabel().setText(ct.toString()); } // end of while } // end of run } // end of class }