如何在Java框架外实现鼠标事件?

时间:2013-12-04 02:38:01

标签: jgrasp

我正在尝试制作一个返回程序,当点击鼠标左键时会按回车键...

礼貌 http://www.java-tips.org/java-se-tips/java.awt/how-to-use-robot-class-in-java.html(用于无效打字方法)和“thenewboston”到目前为止我已经得到了......

我试图让它在其他平台上运行,例如:Word,Note Pad而不仅仅是在JFrame上

这就是我现在所拥有的......

import java.awt.event.MouseEvent;
import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

import java.awt.Robot;
import java.awt.event.KeyEvent;


public class MW3Tool

{

public static void main (String[] args)

{

Robot enter = new Robot();
int num;
return count = 0;

num = count * 3;


Control c = new Control();

for (int k = 1; k <= num; k++)


{System.out.println("H");}


/*  try {
Robot robot = new Robot();                    // Going to be used to electronically hit the enter key later.

  robot.delay(5000);
  robot.setSpeed(10);
  for (int k = 1; k<= num; k ++)
    robot.keyPress(KeyEvent.VK_ENTER);

 }           
  catch (AWTException e) {
   e.printStackTrace();

         }   }    */

}

private class Control implements MouseListener


{
int count;
int useless;
int useless2;
   public void mouseClicked(MouseEvent event) 
{
   count++;
}

public void mousePressed(MouseEvent event) 
{
   useless++;
}


public void mouseExited(MouseEvent event) 
{
   useless2++;
}

}

}

我的错误:

 ----jGRASP exec: javac -g MW3Tool.java

MW3Tool.java:20: cannot return a value from method whose result type is void
return count = 0;
             ^
MW3Tool.java:22: cannot find symbol
symbol  : variable count
location: class MW3Tool
num = count * 3;
      ^
MW3Tool.java:35: non-static variable this cannot be referenced from a static context
Control c = new Control();
            ^
MW3Tool.java:60: MW3Tool.Control is not abstract and does not override abstract method mouseEntered(java.awt.event.MouseEvent) in java.awt.event.MouseListener
private class Control implements MouseListener
        ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

对不起我的低效方法(新加入Java)

任何帮助将不胜感激......谢谢...

1 个答案:

答案 0 :(得分:1)

第一个错误:您是否尝试初始化整数?语法错误。使用

int count = 0;

第二个错误:解决第一个错误将解决此错误 第三个错误:而不是说

private class Control implements MouseListener { ... }

private static class Control implements MouseListener { ... }

上次错误: 请参阅MouseListener Javadocs: 方法摘要
 void mouseClicked(MouseEvent e)           在组件上单击(按下并释放)鼠标按钮时调用。
 void mouseEntered(MouseEvent e)           鼠标进入组件时调用。
 void mouseExited(MouseEvent e)           鼠标退出组件时调用。
 void mousePressed(MouseEvent e)           在组件上按下鼠标按钮时调用。
 void mouseReleased(MouseEvent e)           在组件上释放鼠标按钮时调用。
您必须覆盖Control类中的所有这些方法才能使程序正常工作。
希望这有帮助!