Вы находитесь на странице: 1из 4

package uas; import javax.swing.*; // import library swing pada java import java.awt.

*; // import library AWT pada java import java.awt.event.*; // import library AWT.event pada java import java.text.*; // import library text pada java public class Stopwatch extends JFrame // class stopwatch perluasandari JFrame { static JTextField elapsedTextField = new JTextField(); // construct elapsedTextField static JTextField totalTextField = new JTextField(); // construct totalTextField static JButton startButton = new JButton(); // construct startButton static JButton resetButton = new JButton(); // construct resetButton static JButton exitButton = new JButton(); // construct exitButton static Timer myTimer; static long startTime; // time when Start clicked static long stopTime; // time when Stop clicked static long stoppedTime; // amount of time timer is stopped static boolean reset = true; // true if new timing public static void main(String args[]) { // construct frame new Stopwatch().show(); } public Stopwatch() { // frame constructor setTitle("Stopwatch"); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { exitForm(e); } }); getContentPane().setLayout(new GridBagLayout());

// add text fields GridBagConstraints gridConstraints = new GridBagConstraints(); elapsedTextField.setText("00:00:00.0"); elapsedTextField.setEditable(false); elapsedTextField.setBackground(Color.WHITE); elapsedTextField.setForeground(Color.BLUE); elapsedTextField.setFont(new Font("Arial", Font.BOLD, 24)); elapsedTextField.setColumns(8); elapsedTextField.setHorizontalAlignment(SwingConstants.CENTER); gridConstraints.gridx = 0; gridConstraints.gridy = 0; gridConstraints.gridwidth = 3; getContentPane().add(elapsedTextField, gridConstraints); gridConstraints = new GridBagConstraints(); totalTextField.setText("00:00:00.0");

totalTextField.setEditable(false); totalTextField.setBackground(Color.WHITE); totalTextField.setForeground(Color.RED); totalTextField.setFont(new Font("Arial", Font.BOLD, 24)); totalTextField.setColumns(8); totalTextField.setHorizontalAlignment(SwingConstants.CENTER); gridConstraints.gridx = 0; gridConstraints.gridy = 1; gridConstraints.gridwidth = 3; getContentPane().add(totalTextField, gridConstraints); gridConstraints = new GridBagConstraints(); startButton.setText("Start"); startButton.requestFocus(); startButton.setFont(new Font("Arial", Font.PLAIN, 12)); gridConstraints.gridx = 0; gridConstraints.gridy = 2; gridConstraints.insets = new Insets(5, 5, 0, 0); getContentPane().add(startButton, gridConstraints); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startButtonActionPerformed(e); } }); gridConstraints = new GridBagConstraints(); resetButton.setText("Reset"); resetButton.setFont(new Font("Arial", Font.PLAIN, 12)); resetButton.setEnabled(false); gridConstraints.gridx = 2; gridConstraints.gridy = 2; gridConstraints.insets = new Insets(5, 0, 0, 5); getContentPane().add(resetButton, gridConstraints); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { resetButtonActionPerformed(e); } }); gridConstraints = new GridBagConstraints(); exitButton.setText("Exit"); exitButton.setFont(new Font("Arial", Font.PLAIN, 12)); gridConstraints.gridx = 1; gridConstraints.gridy = 3; gridConstraints.insets = new Insets(5, 0, 5, 0); getContentPane().add(exitButton, gridConstraints); exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { exitButtonActionPerformed(e); } });

pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight()); // create timer myTimer = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { myTimerActionPerformed(e); } }); } private void startButtonActionPerformed(ActionEvent e) { // Starting or restarting timer? if (startButton.getText().equals("Start")) { resetButton.setEnabled(false); exitButton.setEnabled(false); // Reset text on Start/Stop button startButton.setText("Stop"); // Start timer and get starting time if (reset) { reset = false; startTime = System.currentTimeMillis(); stoppedTime = 0; } else { stoppedTime += System.currentTimeMillis() - stopTime; } myTimer.start(); } else { // Stop timer stopTime = System.currentTimeMillis(); myTimer.stop(); // Disable Start/Stop button, enable Reset button startButton.setText("Start"); resetButton.setEnabled(true); exitButton.setEnabled(true); } } private void resetButtonActionPerformed(ActionEvent e) { // Reset displays to zero reset = true; elapsedTextField.setText("00:00:00.0"); totalTextField.setText("00:00:00.0"); // Disable Reset resetButton.setEnabled(false); }

private void exitButtonActionPerformed(ActionEvent e) { exitForm(null); } private void myTimerActionPerformed(ActionEvent e) { long currentTime; // Determine elapsed and total times currentTime = System.currentTimeMillis(); // Display times elapsedTextField.setText(HMS(currentTime - startTime - stoppedTime)); totalTextField.setText(HMS(currentTime - startTime)); } private String HMS(long tms) { int h; int m; double s; double t; t = tms / 1000.0; // Break time down into hours, minutes, and seconds h = (int) (t / 3600); m = (int) ((t - h * 3600) / 60); s = t - h * 3600 - m * 60; // Format time as string return(new DecimalFormat("00").format(h) + ":" + new DecimalFormat("00").format(m) + ":" + new DecimalFormat("00.0").format(s)); } private void exitForm(WindowEvent e) { System.exit(0); } }

Вам также может понравиться