Created
December 1, 2015 18:26
-
-
Save jukbot/66d7b9eed54507e479f5 to your computer and use it in GitHub Desktop.
This file contains 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
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.text.SimpleDateFormat; | |
public class MyNewClock extends JFrame implements ActionListener { | |
JLabel label; | |
JButton button; | |
JButton button2; | |
JButton button3; | |
PrimeTask pt; | |
public MyNewClock(){ | |
super("My new clock"); | |
label = new JLabel(); | |
SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a"); | |
label.setText("Time: " + sdf1.format(new java.util.Date())); | |
button = new JButton("start"); | |
button.addActionListener(this); | |
button2 = new JButton("stop"); | |
button2.addActionListener(this); | |
button3 = new JButton("close"); | |
button3.addActionListener(this); | |
Container c = this.getContentPane(); | |
c.setLayout(new FlowLayout()); | |
c.add(button); | |
c.add(button2); | |
c.add(button3); | |
c.add(label); | |
this.setSize(300,100); | |
this.setLocation(300,300); | |
this.setVisible(true); | |
} | |
public void actionPerformed(ActionEvent ae){ | |
if(ae.getSource().equals(button)){ | |
pt = new PrimeTask(label); | |
pt.execute(); | |
} | |
if(ae.getSource().equals(button2)){ | |
pt.stop(); | |
} | |
if(ae.getSource().equals(button3)){ | |
System.exit(0); | |
} | |
} | |
public static void main(String[] args) { | |
new MyNewClock(); | |
} | |
} | |
class PrimeTask extends SwingWorker{ | |
JLabel lbl; | |
SimpleDateFormat sdf; | |
boolean toRun = true; | |
PrimeTask(JLabel lbl){ | |
this.lbl = lbl; | |
sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a"); | |
} | |
public String doInBackground(){ | |
String s = null; | |
toRun = true; | |
//int count =0; | |
try{ | |
while( toRun) { | |
Thread.currentThread().sleep(1000); | |
java.util.Date d = new java.util.Date(); | |
lbl.setText("Time: " + sdf.format(d)); | |
// count++; | |
} | |
}catch(Exception ex){ | |
ex.printStackTrace(); | |
} | |
return s; | |
} | |
public void stop(){ | |
toRun = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment