实现MouseListener java错误:'('或'['预期

时间:2014-02-03 04:05:46

标签: java swing mouselistener

如何解决此错误:

  

错误:'('或'['   预期................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .................................................. .......................

以下代码:

import java.awt.event.MouseEvent ;
import java.awt.event.MouseListener ;
import javax.swing.JFrame ;
/**
   This class illustrates using a mouse 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, 
   2. Put the reference to the Rectangle component in a constant.
   3. Write the mousePressed method of the listener
   4. When you run the application, click the mouse five times in the
   component, after which the program will exit.
 */
public class RectangleComponentViewerWithMouse
{
    private static final int FRAME_WIDTH = 400 ;
    private static final int FRAME_HEIGHT = 400 ;
    private static final int LIMIT = 7 ;

    public static void main(String[] args)
    {
        //-----------Start below here. To do: approximate lines of code = 12
        // 1. create the RectangleComponent object called component.  
        //Hint: component is used in the inner class
        RectangleComponent component = new RectangleComponent
        //2. class MousePressListener, a MouseListener
        class MousePressListener implements MouseListener
        {//
            //3. an instance variable count starting at 0
            int count = 0;
            //4. public signature for the mousePressed method ; 
            public void actionPerformed(ActionEvent event)
            {//
                //5. increment count
                count++;
                //6. let x (an int) be the x-coordinate of the mouse 
                //obtained by calling getX() on the event object ; 
                int x = event.getX();
                //7. same for y ; 
                int y = event.getY();
                //8. send moveBoxTo message to component giving it x, y
                component.moveBoxTo(x, y);
                //9. when count reaches LIMIT end the application
                if (count == LIMIT)
                    System.exit(0);
            }//
            public void mouseReleased(MouseEvent event) {} //
            public void mouseClicked(MouseEvent event) {} //
            public void mouseEntered(MouseEvent event) {} //
            public void mouseExited(MouseEvent event) {} //
        }//
        //10. make the listener
        MouseListener listener = new MousePressListener();
        //11. addMouseListener to the component
        component.addMouseListener(listener);
        //-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
        JFrame frame = new JFrame() ;
        frame.add(component) ;

        frame.setTitle("Move the box " + LIMIT + " times!") ;
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT) ;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
        frame.setVisible(true) ;
    }
}

RectangleComponentViewerWithMouse.java:31: error: '(' or '[' expected
    class MousePressListener implements MouseListener
    ^
RectangleComponentViewerWithMouse.java:31: error: not a statement
    class MousePressListener implements MouseListener
          ^
RectangleComponentViewerWithMouse.java:31: error: ';' expected
    class MousePressListener implements MouseListener
                            ^
RectangleComponentViewerWithMouse.java:31: error: not a statement
    class MousePressListener implements MouseListener
                                        ^
RectangleComponentViewerWithMouse.java:31: error: ';' expected
    class MousePressListener implements MouseListener
                                                     ^
RectangleComponentViewerWithMouse.java:36: error: illegal start of expression
        public void actionPerformed(ActionEvent event)
        ^
RectangleComponentViewerWithMouse.java:36: error: illegal start of expression
        public void actionPerformed(ActionEvent event)
               ^
RectangleComponentViewerWithMouse.java:36: error: ';' expected
        public void actionPerformed(ActionEvent event)
                                   ^
RectangleComponentViewerWithMouse.java:36: error: ';' expected
        public void actionPerformed(ActionEvent event)
                                                     ^
RectangleComponentViewerWithMouse.java:51: error: illegal start of expression
        public void mouseReleased(MouseEvent event) {} //
        ^
RectangleComponentViewerWithMouse.java:51: error: illegal start of expression
        public void mouseReleased(MouseEvent event) {} //
               ^
RectangleComponentViewerWithMouse.java:51: error: ';' expected
        public void mouseReleased(MouseEvent event) {} //
                                 ^
RectangleComponentViewerWithMouse.java:51: error: ';' expected
        public void mouseReleased(MouseEvent event) {} //
                                                  ^
RectangleComponentViewerWithMouse.java:52: error: illegal start of expression
        public void mouseClicked(MouseEvent event) {} //
        ^
RectangleComponentViewerWithMouse.java:52: error: illegal start of expression
        public void mouseClicked(MouseEvent event) {} //
               ^
RectangleComponentViewerWithMouse.java:52: error: ';' expected
        public void mouseClicked(MouseEvent event) {} //
                                ^
RectangleComponentViewerWithMouse.java:52: error: ';' expected
        public void mouseClicked(MouseEvent event) {} //
                                                 ^
RectangleComponentViewerWithMouse.java:53: error: illegal start of expression
        public void mouseEntered(MouseEvent event) {} //
        ^
RectangleComponentViewerWithMouse.java:53: error: illegal start of expression
        public void mouseEntered(MouseEvent event) {} //
               ^
RectangleComponentViewerWithMouse.java:53: error: ';' expected
        public void mouseEntered(MouseEvent event) {} //
                                ^
RectangleComponentViewerWithMouse.java:53: error: ';' expected
        public void mouseEntered(MouseEvent event) {} //
                                                 ^
RectangleComponentViewerWithMouse.java:54: error: illegal start of expression
        public void mouseExited(MouseEvent event) {} //
        ^
RectangleComponentViewerWithMouse.java:54: error: illegal start of expression
        public void mouseExited(MouseEvent event) {} //
               ^
RectangleComponentViewerWithMouse.java:54: error: ';' expected

2 个答案:

答案 0 :(得分:2)

您忘记在此行()

中添加RectangleComponent component = new RectangleComponent

制作

RectangleComponent component = new RectangleComponent();

答案 1 :(得分:0)

这一行

RectangleComponent component = new RectangleComponent

我猜是30或31,没有分号。