2015-11-19 173 views
-4

這是我用Java編寫的第一個腳本。編譯腳本時出現錯誤。我想知道它有什麼問題。Java:編譯時出現類錯誤

public class Main { 
    package com.omt.mouse; 

    import java.awt.Robot; 
    import java.awt.event.InputEvent; 
    public void mousePressed(MouseEvent e) { 
     if (e.getButton() == MouseEvent.BUTTON1) { 
      mouseDown = true; 
     } 
    } 


    public void mouseReleased(MouseEvent e) { 
     if (e.getButton() == MouseEvent.BUTTON1) { 
      mouseDown = false; 
     } 
    } 

    public static void main(String[] args) { 

     Robot robot = new Robot(); 
     num = 0; 
     while (true) { 
      if (mouseDown) { 
       if (num == 2000) { 
        robot.mousePress(InputEvent.BUTTON1_MASK); 
       } else { 
        num++; 
       } 
      } else if (num == 2000) { 
       num = 0; 
       robot.mouseRelease(InputEvent.BUTTON1_MASK); 
      } 
     } 

    } 
} 

這段代碼應該在持續兩秒後保持左鍵點擊,直到再次按下。

感謝

+0

將您的包裝聲明移到您的類聲明之外,並將您的導入移動到包裝聲明下。這裏的所有都是它的;這只是一個錯字。 – Makoto

+3

順便說一句,這些錯誤應該給你一個關於你的代碼有什麼問題的線索 –

回答

0

該代碼將被編譯,但當然,你應該開始閱讀編譯錯誤來解決這些問題。

package com.omt.mouse; 

import java.awt.*; 
import java.awt.event.InputEvent; 
import java.awt.event.MouseEvent; 

public class Main { 
    static boolean mouseDown; 
    static int num; 

    public static void mousePressed(MouseEvent e) { 
     if (e.getButton() == MouseEvent.BUTTON1) { 
      mouseDown = true; 
     } 
    } 


    public static void mouseReleased(MouseEvent e) { 
     if (e.getButton() == MouseEvent.BUTTON1) { 
      mouseDown = false; 
     } 
    } 

    public static void main(String[] args) throws AWTException { 

     Robot robot = new Robot(); 
     num = 0; 
     while(true) { 
      if(mouseDown) { 
       if(num == 2000) { 
        robot.mousePress(InputEvent.BUTTON1_MASK); 
       } else { 
        num++; 
       } 
      } else if (num == 2000) { 
       num = 0; 
       robot.mouseRelease(InputEvent.BUTTON1_MASK); 
      } 
     } 

    } 
}