2013-07-03 3 views
1

내 프로젝트의 타이머 기반 응용 프로그램을 디자인하려고합니다 (온라인 테스트). 남은 시간을 레이블에 표시해야했습니다. 그래서 String.format을 사용하고있었습니다. 그러나 이클립스가 오류가 있다고 보여줍니다,타이머 응용 프로그램의 인수에 String.format이 적용되지 않습니다.

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


} 
+0

나에게 좋은 것 같다 대해 이야기하지 마십시오 잘 컴파일 잘 실행 ... – MadProgrammer

+0

에는 컴파일 오류가 없습니다 'javac'와 함께 –

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

어떤 단어가 문제인지 ??? – gnanz

+0

이 경우 코드가 완료되고 OP에서 지역 변수 중 하나가 재설정되지 않음 – mKorbel

+0

String 유형의 메서드 형식 (String, Object [])은 인수 (String, int)에 적용 할 수 없습니다. –

관련 문제