When I run this file normally in a JFrame, it works:
package Gadgets;
import java.awt.Button;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.text.*;
import javax.swing.*;
public class timer extends JApplet {
private int time = 0;
public timer() {
NumberFormat twoNumbers = new DecimalFormat("00");
Box timerBox = new Box(BoxLayout.Y_AXIS);
Label time = new Label("00:00:00");
Container options = new Container();
options.setLayout(new BoxLayout(options, BoxLayout.X_AXIS));
Box startBox = new Box(BoxLayout.X_AXIS);
Button start = new Button("Start");
Box stopBox = new Box(BoxLayout.X_AXIS);
Button stop = new Button("Stop");
Box resetBox = new Box(BoxLayout.X_AXIS);
Button reset = new Button("Reset");
timerBox.add(time);
timerBox.setPreferredSize(new Dimension(60, 20));
Timer timer = new Timer(1000, (ActionEvent e) -> {
time.setText(twoNumbers.format(Math.floor(this.time / (60*60))) + ":" + twoNumbers.format(Math.floor(this.time / 60)) + ":" + twoNumbers.format(this.time++ % 60));
});
start.addActionListener((ActionEvent e) -> {
timer.start();
});
stop.addActionListener((ActionEvent e) -> {
timer.stop();
});
reset.addActionListener((ActionEvent e) -> {
timer.stop();
this.time = 0;
time.setText("00:00:00");
});
startBox.add(start);
stopBox.add(stop);
resetBox.add(reset);
options.add(startBox);
options.add(stopBox);
options.add(resetBox);
add(timerBox);
add(options);
}
public static void main(String[] args) {
}
}
However, when I run it in a JApplet, it displays an InvocationTargetException
. When I click 'more details', it doesn't unwrap the exception. What could be possible causes of this?
Thanks in advance.