AT&T; 12:45 PM @* 100% @ D. blackboard.ncat.edu 1 of 1 COMP163 Stopwatch Program
ID: 3889122 • Letter: A
Question
AT&T; 12:45 PM @* 100% @ D. blackboard.ncat.edu 1 of 1 COMP163 Stopwatch Program W'rite a Java peogram with a Graphical User Imerface that will function as a stopwatch. The GUI enly meeds a button and a label to display the clapsed time. . 2.81 seconds The program will need a class itstance variable called startTise to keep track of the time when the start button was pressod. This value should be a long, which is just like an int, but can stoee mch larger numbers. It should be initialized to zeo The actionPerformed method should be structured as shown below. Note in the IF statement there are two equals signs public void actionPerforsed(java . awt.event .ActionEvent thing> W create an object of the java.util.Date class W call the getTine 0 method on the Date object and save the retumed value in starttine W change the test of the bun else // create an object of the ] ava·ut i 1 , Date class W call the gertine (0 method on the Date object. Suberact startTine from the returned valuc to get the elapsed time. Convert the clapsed time to a double and divide by 1000 to get seconds W Display the result Wchange the test of the button set startTine to zeroExplanation / Answer
NOTE: Please, complile and run the following code in Netbeans IDE. Steps:
Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package stopwatch;
import java.util.Date;
/**
*
* @author saksham
*/
public class Stopwatch extends javax.swing.JFrame {
/**
* Creates new form Stopwatch
*/
public Stopwatch() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Start");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(82, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(44, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String buttonText = jButton1.getText();
Date date = new Date();
long startTime=0,stopTime=0;
if("Start".equals(buttonText)){
startTime = date.getTime();
jButton1.setText("Stop");
jLabel1.setText("running");
}else if("Stop".equals(buttonText)){
stopTime = date.getTime();
long elapsedTime = stopTime-startTime;
jButton1.setText("Start");
jLabel1.setText(((double)elapsedTime)/1000+" seconds");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Stopwatch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Stopwatch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Stopwatch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Stopwatch.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Stopwatch().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}