【发布时间】:2013-07-03 06:22:16
【问题描述】:
我正在尝试为我的项目(在线测试)设计一个基于计时器的应用程序。我必须在标签中显示剩余时间。所以我使用的是String.format。但是 Eclipse 显示这是一个错误:
String 类型中的方法 format(String, Object[]) 不适用于参数 (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();
}
}
【问题讨论】:
-
对我来说似乎很好,编译良好,运行良好......
-
javac没有编译错误 -
在我身边也很好运行
-
它在我的同事系统中运行良好