<small id='Nf4JY'></small><noframes id='Nf4JY'>

    1. <tfoot id='Nf4JY'></tfoot>

        <bdo id='Nf4JY'></bdo><ul id='Nf4JY'></ul>
      <i id='Nf4JY'><tr id='Nf4JY'><dt id='Nf4JY'><q id='Nf4JY'><span id='Nf4JY'><b id='Nf4JY'><form id='Nf4JY'><ins id='Nf4JY'></ins><ul id='Nf4JY'></ul><sub id='Nf4JY'></sub></form><legend id='Nf4JY'></legend><bdo id='Nf4JY'><pre id='Nf4JY'><center id='Nf4JY'></center></pre></bdo></b><th id='Nf4JY'></th></span></q></dt></tr></i><div id='Nf4JY'><tfoot id='Nf4JY'></tfoot><dl id='Nf4JY'><fieldset id='Nf4JY'></fieldset></dl></div>
    2. <legend id='Nf4JY'><style id='Nf4JY'><dir id='Nf4JY'><q id='Nf4JY'></q></dir></style></legend>

      1. Java - Swing 中的 mouseMoved() 事件处理

        时间:2023-09-29
          <legend id='j0Hqu'><style id='j0Hqu'><dir id='j0Hqu'><q id='j0Hqu'></q></dir></style></legend>

          <small id='j0Hqu'></small><noframes id='j0Hqu'>

              <tfoot id='j0Hqu'></tfoot>

                <bdo id='j0Hqu'></bdo><ul id='j0Hqu'></ul>
                1. <i id='j0Hqu'><tr id='j0Hqu'><dt id='j0Hqu'><q id='j0Hqu'><span id='j0Hqu'><b id='j0Hqu'><form id='j0Hqu'><ins id='j0Hqu'></ins><ul id='j0Hqu'></ul><sub id='j0Hqu'></sub></form><legend id='j0Hqu'></legend><bdo id='j0Hqu'><pre id='j0Hqu'><center id='j0Hqu'></center></pre></bdo></b><th id='j0Hqu'></th></span></q></dt></tr></i><div id='j0Hqu'><tfoot id='j0Hqu'></tfoot><dl id='j0Hqu'><fieldset id='j0Hqu'></fieldset></dl></div>
                    <tbody id='j0Hqu'></tbody>

                2. 本文介绍了Java - Swing 中的 mouseMoved() 事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想在我的 JFrame 中监听鼠标移动和点击.为此,我添加了一个像这样实现的 MouseListener:(View 类的整个代码位于 https://gist.github.com/2837224,Board 类在 https://gist.github.com/2837231)

                  I want to listen for mouse movements and clicks in my JFrame. To do this, I've added a MouseListener implemented like this: (whole code of View class is at https://gist.github.com/2837224, Board class is at 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() 运行良好,但其他两个却没有(移动鼠标时控制台上没有显示).谁能告诉我我做错了什么?提前致谢!

                  Funny thing is that mousePressed() is working well, but the other two are not (nothing shows on console while moving the mouse). Anybody could tell me what am I doing wrong? Thanks in advance!

                  推荐答案

                  如果要接收 mouseMovedmouseDragged 事件,还需要将监听器注册为MouseMotionListener.

                  If you want to receive mouseMoved and mouseDragged events, you need to also register your listener as a MouseMotionListener.

                  MouseAdapter adapter = ...
                  JFrame frame = ...
                  frame.addMouseListener(adapter);
                  frame.addMouseMotionListener(adapter);
                  

                  这篇关于Java - Swing 中的 mouseMoved() 事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:访问java内部类中的变量 下一篇:JSpinner 值变化事件

                  相关文章

                  最新文章

                3. <i id='gtKAL'><tr id='gtKAL'><dt id='gtKAL'><q id='gtKAL'><span id='gtKAL'><b id='gtKAL'><form id='gtKAL'><ins id='gtKAL'></ins><ul id='gtKAL'></ul><sub id='gtKAL'></sub></form><legend id='gtKAL'></legend><bdo id='gtKAL'><pre id='gtKAL'><center id='gtKAL'></center></pre></bdo></b><th id='gtKAL'></th></span></q></dt></tr></i><div id='gtKAL'><tfoot id='gtKAL'></tfoot><dl id='gtKAL'><fieldset id='gtKAL'></fieldset></dl></div>

                  <small id='gtKAL'></small><noframes id='gtKAL'>

                      <legend id='gtKAL'><style id='gtKAL'><dir id='gtKAL'><q id='gtKAL'></q></dir></style></legend>
                        <bdo id='gtKAL'></bdo><ul id='gtKAL'></ul>
                      <tfoot id='gtKAL'></tfoot>