当我在 JFrame 中正常运行此文件时,它可以工作:
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) {
}
}
但是,当我在 JApplet 中运行它时,它会显示一个InvocationTargetException
. 当我单击“更多详细信息”时,它不会解开异常。这可能是什么原因?
提前致谢。