I am trying to design a timer based application for my project (Online Test). I had to display the remaining time in a label. So I was using String.format. But Eclipse shows that it's an error:
The method format(String, Object[]) in the type String is not applicable for the arguments (String, int)).
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Formatter;
import javax.swing.*;
public class RadioButton {
JFrame frame=new JFrame("RadioRadio");
JLabel timerl = new JLabel("Press Button to start");
JPanel butp = new JPanel();
JButton button = new JButton("Start Exam");
Timer mytimer;
String ss="Time Remaining %02d Seconds!";
int elapsedSeconds = 0;
int total=10;
public void radioButton()
{
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(butp);
butp.add(button);
butp.add(timerl);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (mytimer != null && mytimer.isRunning()) {
mytimer.stop();
mytimer = null;
timerl.setText("Exam Terminated");
} else {
mytimer = new Timer(1000, new TimerListener());
mytimer.start();
String t = String.format(ss, total);
timerl.setText(t);
}
}
});
}
private class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
elapsedSeconds++;
if (elapsedSeconds == total) {
mytimer.stop();
timerl.setText("Time Up");
} else {
String t = String.format(ss, total - elapsedSeconds);
timerl.setText(t);
}
}
}
public static void main(String args[])
{
RadioButton r=new RadioButton();
r.radioButton();
}
}