1

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();
    }


}
4

1 回答 1

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();
            }
        });
    }
}
于 2013-07-03T06:35:01.797 回答