// JavaProject Stopwatch
// PFrame.java
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
class ValLabel extends JLabel {
private static final long serialVersionUID = 0L;
public ValLabel() {
setFont(new Font("Courier", Font.PLAIN, 55));
}
}
class TxtLabel extends JLabel {
private static final long serialVersionUID = 0L;
public TxtLabel() {
setFont(new Font("Arial", Font.PLAIN, 16));
}
}
class PFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 0L;
JPanel ptop, pmiddle, pbottom; //
Bezeichner
JButton bstart, bstop, breset;
ValLabel lhrs, lmin, lsec;
TxtLabel lhs, lmn, lsc;
Timer timer;
int ctr = FlowLayout.CENTER;
public PFrame(int width, int height) {
setTitle("Stoppuhr");
Image icon = Toolkit.getDefaultToolkit().getImage("dh.png");
setIconImage(icon);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 1));
ptop = new JPanel();
add(ptop);
pmiddle = new JPanel();
add(pmiddle);
pbottom = new JPanel();
add(pbottom);
ptop.setLayout(new FlowLayout(ctr, 40, 55));
bstart = new JButton("START");
bstart.addActionListener(this);
bstop = new JButton("STOP");
bstop.addActionListener(this);
breset = new JButton("RESET");
breset.addActionListener(this);
ptop.add(bstart);
ptop.add(bstop);
ptop.add(breset);
pmiddle.setLayout(new FlowLayout(ctr, 20, 18));
lhrs = new ValLabel();
lmin = new ValLabel();
lsec = new ValLabel();
pmiddle.add(lhrs);
pmiddle.add(lmin);
pmiddle.add(lsec);
pbottom.setLayout(null);
lhs = new TxtLabel();
lmn = new TxtLabel();
lsc = new TxtLabel();
lhs.setText("hrs");
lhs.setBounds(70, 0, 50, 20);
lmn.setText("min");
lmn.setBounds(155, 0, 50, 20);
lsc.setText("sec");
lsc.setBounds(240, 0, 50, 20);
pbottom.add(lhs);
pbottom.add(lmn);
pbottom.add(lsc);
timer = new Timer(this);
}
public void reset() {
lhrs.setText("00");
lmin.setText("00");
lsec.setText("00");
}
public void actionPerformed(ActionEvent object) {
if (object.getSource() == bstart) timer.start();
if (object.getSource() == bstop) timer.stop();
if (object.getSource() == breset) {
timer.stop();
reset();
}
}
}
// Timer.java
public class Timer implements Runnable {
PFrame pfr;
Thread runtime;
int hrs, min, sec;
boolean isRunning = false;
public Timer(PFrame pfr) {
this.pfr = pfr;
}
@Override
public void run() {
while (isRunning) {
try {
sec ++;
if (sec == 60) {
sec = 0;
min += 1;
if (min == 60) {
min = 0;
hrs += 1;
}
}
if (sec < 10) pfr.lsec.setText("0"+sec);
else pfr.lsec.setText(""+sec);
if (min < 10) pfr.lmin.setText("0"+min);
else pfr.lmin.setText(""+min);
if (hrs < 10) pfr.lhrs.setText("0"+hrs);
else pfr.lhrs.setText(""+hrs);
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
}
public void start() {
if (!isRunning) {
runtime =
new Thread(this);
hrs = 0;
min = 0;
sec = 0;
isRunning = true;
runtime.start();
}
}
public void stop() {
if (isRunning) {
runtime.interrupt();
isRunning = false;
}
}
}
// Stopwatch.java
public class Stopwatch {
PFrame pfr;
public Stopwatch() {
pfr = new PFrame(349, 301);
pfr.reset();
pfr.setVisible(true);
}
public static void main(String[] args) {
new Stopwatch();
}
}
Download Stopwatch