Forgive me I am still really new to Java. I have this swing application which creates a small JFrame that when clicked, counts down from 2 minutes and stops at zero. I build a GUI in netbeans IDE and I read that in order to place this timer code into one of the JFrames on the GUI i would need to change this to an applet.
I don't really know how to go about this.
import javax.swing.JApplet;
public class Scoreclock extends JApplet {
}
import javax.swing.JApplet; public class Stopwatch extends JApplet { }
Netbeans generated this code while i tried to start of creating an Applet in my GUI package. when I inserted the code into the { } it came up with a build error. How would I go about doing this?
The code for my timer:
//***************Import java files necessary for this project********************//
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
//******************************************************************************//
public class Stopwatch extends JFrame implements ActionListener, Runnable
{
// (25/15)=1.66
int secondCount = 200; //integer must be a multiple of 25
public long timeAdjust = (6200-(secondCount))*60*10;
private long startTime;
private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss : SSS");
private final JButton startStopButton= new JButton("Start/stop");
private Thread updater;
private boolean isRunning= false;
private final Runnable displayUpdater= new Runnable()
{
public void run() // Here
{
displayElapsedTime(Math.max(Stopwatch.this.startTime - System.currentTimeMillis(),-timeAdjust));
}
};
//************************Action Performed Function***********************//
public void actionPerformed(ActionEvent ae)
{
if(isRunning)
{
long elapsed= startTime - System.currentTimeMillis() ;
isRunning= false;
try
{
updater.join();
// Wait for updater to finish
}
catch(InterruptedException ie) {}
displayElapsedTime(elapsed);
// Display the end-result
}
else
{
//*********************Start Command for the Function*********************//
/*
* Here I have built a function so that I can adjust secondCount to a number
* and it will change the timer. When secondCount is at 25, the timer is displayed as
* 00:15:000 in mm:ss:sss format. When secondCount is at 200, the timer is displayed
* as 02:00:000 in mm:ss:sss format.
*/
startTime= 2*60*1000+System.currentTimeMillis()-timeAdjust;
isRunning = true;
updater= new Thread(this);
updater.start();
//***********************************************************************//
}
}
//*********************Action Performed Function Ends**********************//
//**************************Display Time Function**************************//
private void displayElapsedTime(long elapsedTime)
{
startStopButton.setText(timerFormat.format(new java.util.Date(elapsedTime)));
}
//***********************Display Time Function Ends***********************//
//**********************Function to Run the Timer*************************//
public void run()
{
try
{
while(isRunning)
{
SwingUtilities.invokeAndWait(displayUpdater);
Thread.sleep(50);
}
}
catch(java.lang.reflect.InvocationTargetException ite)
{
ite.printStackTrace(System.err);
// Should never happen!
}
catch(InterruptedException ie) {}
// Ignore and return!
}
//************************Function to Run Timer Ends************************//
//**********************Create the StartStop Button*************************//
public Stopwatch()
{
startStopButton.addActionListener(this);
getContentPane().add(startStopButton);
setSize(100,50);
setVisible(true);
}
//********************Create the StartStop Button Ends***********************//
public static void main(String[] arg)
{
new Stopwatch().addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}