Java错误:<identifier>期望的timer.start();

fir*_*hip 2 java timer

我该如何解决这个错误?

错误:预期的timer.start()

import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.JFrame ;
import javax.swing.Timer ;
/**
   This class illustrates using a Timer with a listener
   The listener illustrates using an interface.
   The program also illustrates that the inner class has access
   to constants in the local environment, but not variables.

   This is an example from the book, so the answer is there.
   Your job is 
   1. write the line that makes the Rectangle component, 
      remembering that it will have to be a constant.
   2. write the listener so that every time the timer ticks, it 
      moves the rectangle in the component by (dx, dy) = (DELTA, DELTA).
 */
public class RectangleComponentViewerWithTimer
{
    private static final int FRAME_WIDTH = 300 ;
    private static final int FRAME_HEIGHT = 400 ;
    private static final int DELTA = 2 ;
    private static final int LIMIT = 30 ;
    private static final int INTERVAL = 400 ;

    public static void main(String[] args)
    {
        //-----------Start below here. To do: approximate lines of code = 12
        // 1. create the RectangleComponent1 object called component.  
        //Hint: component will be used in the inner class
        RectangleComponent1 component = new RectangleComponent1();
        //1. define class TimerListener, an ActionListener
        class TimerListener implements ActionListener
        {//
            //2. instance variable count starting at zero ; 
            int count = 0;
            //3. actionPerformed method signature ; 
            public void actionPerformed(ActionEvent event)
            {//
                //4. increment count ; 
                count++;
                //5. tell component to moveBoxBy DELTA and DELTA ; 
                component.moveBoxBy(DELTA, DELTA);
                //6. when count reaches the LIMIT 
                if (count == LIMIT)
                    //7. print component.toString() ; 
                    System.out.println(component.toString());
                    //8. stop the application
                    System.exit(0);
                }//
            }//
        }//
        //9. make the listener
        ActionListener listener = new TimerListener();
        //10. make the timer based on INTERVAL and listener
        Timer timer = new Timer(INTERVAL, listener);
        //11. start the timer
        timer.start() ;
        //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
        JFrame frame = new JFrame() ;
        frame.add(component) ;
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
        frame.setTitle("Animated rectangle.") ;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
        frame.setVisible(true) ;
    }
}

RectangleComponentViewerWithTimer.java:58: error: <identifier> expected
    timer.start() ;
               ^
RectangleComponentViewerWithTimer.java:61: error: <identifier> expected
    frame.add(component) ;
             ^
RectangleComponentViewerWithTimer.java:61: error: <identifier> expected
    frame.add(component) ;
                       ^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
                 ^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
                             ^
RectangleComponentViewerWithTimer.java:62: error: <identifier> expected
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
                                           ^
RectangleComponentViewerWithTimer.java:63: error: <identifier> expected
    frame.setTitle("Animated rectangle.") ;
                  ^
RectangleComponentViewerWithTimer.java:63: error: illegal start of type
    frame.setTitle("Animated rectangle.") ;
                   ^
RectangleComponentViewerWithTimer.java:64: error: <identifier> expected
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
                                  ^
RectangleComponentViewerWithTimer.java:64: error: <identifier> expected
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
                                                       ^
RectangleComponentViewerWithTimer.java:65: error: <identifier> expected
    frame.setVisible(true) ;
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 6

你看起来很糟糕的代码格式化.匹配你的花括号,你会发现你有一些方法之外的代码.

你的格式化导致你认为你有一个大括号,而你没有:

            if (count == LIMIT)  // ***** no curly brace here *****
                //7. print component.toString() ; 
                System.out.println(component.toString());
                //8. stop the application
                System.exit(0);
            }   // **** this does not belong to the if block!
Run Code Online (Sandbox Code Playgroud)

因此,从ActionListener listener = new TimerListener();任何方法或构造函数之外的所有代码都是赤裸裸地坐在类中.

关键点:代码格式化不是关于漂亮的代码,而是关于可调试代码.

其他记录:

  • 如果您可以使用NetBeans,Eclipse或Intellij等IDE,请考虑这样做.虽然这些人有学习曲线,但最终还是值得的,因为他们动态编译你的代码并立即告诉你是否有编译错误.
  • 无论如何,您应经常检查编译错误.
  • 当您找到一个时,在修复所有编译问题之前,不应向程序中添加任何新代码.否则你最终可能会遇到问题,你最终解决了一个问题,然后再弹出5个问题.