我正在尝试为我的项目设计基于计时器的应用程序(在线测试)。我必须在标签中显示剩余时间。所以我使用的是String.format。但Eclipse表明这是一个错误:
String类型中的方法格式(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();
}
}
答案 0 :(得分:1)
请不要告诉任何人
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyRadioButton {
private JFrame frame = new JFrame("RadioRadio");
private JLabel timerl = new JLabel("Press Button to start");
private JPanel butp = new JPanel();
private JButton button = new JButton("Start Exam");
private Timer mytimer;
private String ss = "Time Remaining %02d Seconds!";
private int elapsedSeconds = 0;
private int total = 10;
public MyRadioButton() {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (mytimer != null && mytimer.isRunning()) {
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Exam Terminated");
} else {
mytimer = new Timer(1000, new TimerListener());
mytimer.start();
String t = String.format(ss, total);
timerl.setText(t);
}
}
});
butp.add(button);
butp.add(timerl);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(butp);
frame.setSize(400, 300);
frame.setVisible(true);
}
private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
elapsedSeconds++;
if (elapsedSeconds == total) {
mytimer.stop();
elapsedSeconds = 0;
timerl.setText("Time Up");
} else {
String t = String.format(ss, total - elapsedSeconds);
timerl.setText(t);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyRadioButton r = new MyRadioButton();
}
});
}
}