public void method1(int x){ if(x == 1){ 尝试{ 机器人r =新机器人(); } catch(AWTException e){ // TODO自动生成的catch块 Ë….
public void method1 (int x) { if (x == 1) { try { Robot r = new Robot(); // R is defined in the try block. It is not visible outside of this block. } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); } r.mouseMove(50,50); // R is not defined anymore here
替换为
public void method1 (int x) { if (x == 1) { try { Robot r = new Robot(); r.mouseMove(50,50); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); }
它应该是:
public void method1 (int x) { Robot r = null; if (x == 1) { try { r = new Robot(); } catch (AWTException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } r.mouseMove(50,50);