当前位置: 首页 > news >正文

java中mouseMoved_Java – Swing中的mouseMoved()事件处理

我想听听JFrame中的鼠标移动和点击.为此,我添加了一个像这样实现的MouseListener 🙁 View类的整个代码是

https://gist.github.com/2837224,Board类是

https://gist.github.com/2837231)

class BattleshipsFrame extends JFrame {

private final Board playerBoard, opponentBoard;

private View view;

/** Main window constructor. */

BattleshipsFrame() {

...

...

...

//creating and displaying boards

playerBoard = new Board();

opponentBoard = new Board();

PlayerBoardListener mouseListener = new PlayerBoardListener();

this.addMouseListener(mouseListener);

playerBoard.addMouseListener(mouseListener);

opponentBoard.addMouseListener(new OpponentBoardListener());

boards.add(playerBoard);

boards.add(opponentBoard);

...

...

...

}

/** Listener responsible for handling mouse events on player board */

private class PlayerBoardListener extends MouseAdapter {

@Override public void mousePressed(MouseEvent event) {

try {

if(event.getButton() == MouseEvent.BUTTON1 && controllerConnection != null)

controllerConnection.sendShipPlacedEvent(event.getX()/40, event.getY()/40, ShipType.EMPTY);

} catch(Exception e) {

e.printStackTrace();

}

}

@Override public void mouseMoved(MouseEvent event) {

//TODO

System.out.println("Mouse movement detected! Actual mouse position is: " + event.getX()+ "," + event.getY() + ".");

}

@Override public void mouseDragged(MouseEvent event) {

//TODO

System.out.println("Mouse movement detected! Actual mouse position is: " + event.getX()+ "," + event.getY() + ".");

}

}

有趣的是,mousePressed()运行良好,但其他两个都没有(移动鼠标时控制台上没有显示).谁能告诉我我做错了什么?提前致谢!

相关文章:

  • php的hashmap,php如何实现hashmap
  • php创建表格,php 创建表
  • php 执行流程图,PHP底层设计及执行流程
  • java 过期缓存,带有过期密钥的Java基于时间的映射/缓存
  • php中两个变量都是string_内容也一样但是长度不一样,php基础知识期末考试题无答案...
  • java两个子线程通信,java – 两个线程如何在两个线程运行时进行通信?
  • php7 phpunittest,php - 如何用phpunit运行单一测试方法?
  • zabbix监控php,zabbix监控php状态
  • php语言的form,PHP语言 HTTP请求类,支持GET,POST,Multipart、form-data
  • JAVA定义一个狗看门,为什么我用接口实现狗看门的功能出错?
  • 查看php-fpm配置文件,php-fpm 配置文件检测
  • 用php查ip,php – 有效的ip-位置查询
  • php 怎么取出一个最大值,php max() min() 返回最大值和最小值
  • 公示系统php,企业信用查询公示系统
  • oracle判断单数双数,代码中你所不知道的偶数与奇数的判断方法