【问题标题】:Java Focus Event ListenerJava 焦点事件监听器
【发布时间】:2016-05-03 07:21:29
【问题描述】:

我想知道如何添加获得焦点的事件侦听器。

目前我有一个鼠标事件被添加到我的 JTextareas

   //=======================================================
   // mouse drag event
   //=======================================================

   public static class genDrag extends MouseMotionAdapter {
      JTextArea textarea;

      // receive textarea as argument
      public genDrag(JTextArea argTextarea) {
         textarea = argTextarea;
      }

      // add drag functionality to argument
      public void mouseDragged(MouseEvent E) {
         Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
         textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
      }
   }

然后我可以调用它

   //=======================================================
   // apply mouse event
   //=======================================================
   JTextArea textarea = new JTextArea();
   textarea.setBounds(50, 50, 100, 30);
   textarea.addMouseMotionListener(new genDrag(textarea));

这工作正常,但我一直无法重现相同 focusGained 事件的功能

   //=======================================================
   // mouse focus event
   //=======================================================

   public static class genFocus extends EventListener {
      JTextArea textarea;

      public genFocus() {
         textarea = argTextarea;
      }

      public void focusGained(FocusEvent E) {
        System.out.println("Focus Triggered");
      }
   }

以上看起来一点都不开心

更新代码

static gui classGui;

public static void main(String[] args) {

   classGui = new gui();

   classGui.textarea.addMouseMotionListener(
       new genDrag(classGui.textarea)
   );

   classGui.textarea.addFocusListener(
      new genFocus(this)
   );

   classGui.frame.setVisible(true);

   public static class gui {
       JFrame frame;
       JPanel panel;
       JTextArea textarea;

       public gui() {
           frame = new JFrame();
           // configure JFrame here

           panel = new JPanel();
           // configure JPanel here

           textarea = new JTextArea();
           textarea.setBounds(50, 50, 100, 30);

           frame.add(textarea);
      }

   }

   public static class genDrag extends MouseMotionAdapter {
       JTextArea textarea;

       public genDrag(JTextArea argTextarea) {
           textarea = argTextarea;
       }

       public void mouseDragged(MouseEvent E) {
           Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
           textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
       }
   }

   public static class genFocus implements FocusListener {
       JTextArea textarea;

       public genFocus(JTextArea argTextarea) {
           textarea = argTextarea;
       }

       public void focusGained(FocusEvent E) {
          System.out.println("Focus gained");
       }

       public void focusLost(FocusEvent E) {
          System.out.println("Focus lost");
       }
   }

}

【问题讨论】:

    标签: java events focus


    【解决方案1】:

    要处理焦点事件,您的处理程序需要实现FocusListener 接口而不是EventListener。 请注意,您需要通过addFocusListener 添加此处理程序。我不认为你这样做了,因为如果你这样做了,你会得到一个编译器错误,指出哪里出了问题。

    使用@Override 注释有助于发现此类错误。将它放在您认为应该覆盖父方法的每个方法之上。如果这样的方法实际上没有覆盖另一个方法,编译器将抛出错误。通过这种方式,您可以得知错误,而不是让您的程序静默失败。

    【讨论】:

    • 我想我已经做出了你建议的改变
    • No o_o 它不喜欢 genFocus 类
    • @Trent:FocusListener 有两个方法,focusGained 和 focusLost。如果你不实现这两个,你会得到一个编译器错误,因为在实现一个接口时,你需要实现它的所有方法。其他处理程序不会发生这种情况,因为您在那里扩展了一个类。
    • 我添加了 focusLost 位
    • 现在可以用了吗?如果没有,发生了什么?如果出现编译器错误,是哪一个?仅从代码中很难猜出问题所在。
    【解决方案2】:

    您应该将event-listener 添加到控件JTextArea 然后只有它才能处理任何event 请求。

    JTextField textarea= new JTextField("Value");
    textarea.addFocusListener(new genFocus(textarea)); //this peice of code will add an listener to you textarea Object of JTextField.
    

    您的鼠标侦听器将工作,因为您已将鼠标事件侦听器添加到您的 JTextArea

    textarea.addMouseMotionListener(new genDrag(textarea));//code to add MouseMotionListener.
    

    但是没有FocusEvent注册到你的JTextArea

    谢谢。

    【讨论】:

    • 我已经更新我的代码更接近我认为你的意思
    【解决方案3】:

    我认为this 正是您所需要的......

    提示:您的类 genFocus(更喜欢遵循代码约定:GenFocus)应该实现 FocusListener。

    【讨论】:

    • @Trent 你的代码根本不应该编译——你还没有实现focusLost。此外,您的 ctor 与您的称呼不匹配...
    • 不,它不编译,我不知道我需要实现 focusLost
    • 我添加了 focusLost 位
    • @Trent 这是接口所固有的——它们的所有功能都是隐式抽象的。如果任何函数是抽象的(继承的或显式定义的),则无法实例化一个类。相反,子类必须自己提供适当的实现,然后才能实例化。
    • @Trent 好吧,你已经有了一个:FocusListener。它是一个接口,这就是为什么您必须使用“实现”而不是扩展的原因。作为一个接口,您必须实现所提供的两种方法,focusGained 和 focusLost - 正如您现在所做的那样,现在看起来很好。遗留问题:new genFocus(this)。我不认为您的包含 main 的类继承自 JTextArea - 是吗?如果是这样,编译失败,如果它实际上继承,你监视另一个 JTextArea 而不是你想要的。像你对鼠标监听器所做的那样做new genFocus(classGui.textArea)
    猜你喜欢
    • 2019-08-26
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多