Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. package stopwatch1;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import javax.swing.*;
  8.  
  9. public class WatchGUI extends JFrame implements ActionListener{
  10. private StopWatch myWatch; //need a reference to signal input
  11. private JTextArea textArea; //write status text
  12. private JLabel timeLabel; //the time
  13.  
  14. public WatchGUI(StopWatch sWatch){
  15. myWatch=sWatch;
  16.  
  17. setTitle("StopWatch");
  18. setSize(500,300);
  19.  
  20. //ctreate buttons to control stopwatch
  21. JButton startButton=new JButton("Start");
  22. JButton stopButton=new JButton("Stop");
  23. JButton resetButton=new JButton("Reset");
  24. JPanel buttonPanel = new JPanel();
  25. buttonPanel.add(startButton);
  26. buttonPanel.add(stopButton);
  27. buttonPanel.add(resetButton);
  28. add(buttonPanel,BorderLayout.NORTH);
  29. //add listener to each button
  30. GUIListener startAction= new GUIListener("Start");
  31. startButton.addActionListener(startAction);
  32. GUIListener stopAction= new GUIListener("Stop");
  33. stopButton.addActionListener(stopAction);
  34. GUIListener resetAction= new GUIListener("Reset");
  35. resetButton.addActionListener(resetAction);
  36.  
  37. //add a textArea to print status
  38. JPanel tPanel= new JPanel();
  39. textArea = new JTextArea(10,44);
  40. tPanel.add(textArea);
  41. add(tPanel,BorderLayout.SOUTH);
  42. timeLabel=new JLabel("00:00:00.0");
  43. timeLabel.setFont(new Font("Serif", Font.PLAIN, 48));
  44. JPanel timePanel= new JPanel();
  45. timePanel.add(timeLabel);
  46. add(timePanel,BorderLayout.CENTER);
  47. }
  48.  
  49. public void writeStatus(String text){
  50. textArea.append(text);
  51. }
  52.  
  53. public void writeTime(String time){
  54. timeLabel.setText(time);
  55. }
  56.  
  57. @Override
  58. public void actionPerformed(ActionEvent e) {
  59. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  60. }
  61.  
  62. private class GUIListener implements ActionListener {
  63. private String text;
  64.  
  65. public GUIListener(String message){
  66. text=message;
  67. }
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent event){
  71. //notify in status that a button is clicked
  72. writeStatus(getCurrentTimeStamp());
  73. writeStatus(text);
  74. writeStatus(" button pressedn");
  75. //notify my stopwatch that a button ha been clocked
  76. if(text=="Start")
  77. myWatch.start();
  78. if(text=="Stop")
  79. myWatch.stop();
  80. if(text=="Reset")
  81. myWatch.reset();
  82. }
  83.  
  84. public String getCurrentTimeStamp() {
  85. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//dd/MM/yyyy
  86. Date now = new Date();
  87. String strDate = sdfDate.format(now);
  88. strDate+=": ";
  89. return strDate;
  90. }
  91. }
  92. }
  93.  
  94.  
  95. package stopwatch1;
  96.  
  97. import java.awt.EventQueue;
  98. import javax.swing.JFrame;
  99.  
  100.  
  101. //the actual stopwatch
  102. public class StopWatch {
  103.  
  104.  
  105. private WatchGUI myGui;
  106. private int hours;
  107. private int min;
  108. private int sec;
  109. private int dsec; //1/10 second
  110.  
  111. public StopWatch(){
  112. //initiate time
  113. hours=0;
  114. min=0;
  115. sec=0;
  116. dsec=0;
  117. //create GUI
  118. myGui = new WatchGUI(this);
  119. EventQueue.invokeLater(new Runnable()
  120. {
  121. public void run()
  122. {
  123. myGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  124. myGui.setVisible(true);
  125. }
  126. });
  127. }
  128.  
  129. public void start(){
  130. System.out.println("start");
  131. }
  132.  
  133. public void stop(){
  134. System.out.println("stop");
  135. }
  136.  
  137. public void reset(){
  138. System.out.println("reset");
  139. }
  140.  
  141. public void tick(){
  142. System.out.println("tick");
  143. }
  144. }
  145.  
  146.  
  147. package stopwatch1;
  148.  
  149. import java.util.logging.Level;
  150. import java.util.logging.Logger;
  151.  
  152.  
  153. public class MyThread extends Thread {
  154.  
  155. @Override
  156. public void run(){
  157. }
  158. }
  159.  
  160.  
  161. package stopwatch1;
  162.  
  163. public class TheWatch {
  164.  
  165.  
  166. public static void main(String[] args) {
  167. StopWatch swatch= new StopWatch();
  168. MyThread t = new MyThread();
  169. t.start();
  170.  
  171. }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement