×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

It seems that your concept is not quite clear here

1. The following toy code is OK:

public class XXX extends JPanel implements MouseMotionListener{

JButton jButton = new jButton("Test");
jButton.addMouseMotionListener(this);

public void mouseDrapped(MouseEvent e){
//Do this
}
public void mouseMoved9MouseEvent e){
//Do that
}

}
Comments: "JButton.this" is at least a poor syntax.

2. In Java gragramming, it is a common practice to have a Container be an EventListener. But it is not a good idea to let Button or JButton be a listener. Academically, it is OK.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / java problem
    here is the application of java.when it compile ,what will be complained by compiler?Why?what's the deference between using the ActionListener and MouseMotionListener?
    How to apply the function that listen the mouse motion event by the button?

    why?
    import java.awt.event.*;
    import javax.swing.*;

    public class A extends JPanel{
    JButton jb=new JBUtton("test");
    JTextField txt=new JTextField(20);
    jb.addMouseMotionListener(new MouseMotionListener(){
    public void mouseDrapped(MouseEvent e){}
    public void mouseMoved9MouseEvent e){
    txt.setText("Done!");
    }
    });
    public static void main(String[] args){
    JFrame frame=new JFrame();
    frame.getContentPane().add(new A());
    frame.setSize(300,200);
    frame.setVisible(true);
    }
    }
    • You can click on a Button (ActionEvent). And You can move your mouse on a Container such as Panel (Applet is a kind of Panel), Frame, Window.
      Dear OldSix,

      Moving a mouse on a Button is not a meaning event. So you should not add
      a MouseMotionListener to the Jbutton.
      Instead, you should add an ActionListener.

      For a Container such as Applet, you can add a MouseMotionListener.

      Hope it helps you.
      • hi,jabber,i am wondered...
        certianly,it's just for test.but i am wonder now,
        in JDK document ,the JButton has the method inherited from Component, so it should be useful to JButton. that's the question is:
        1.How to use the inherited method?
        Is there any other rule in using the inherited method?
        by the way,i define the class implement the mousemotionlistener, in there,i add the line:
        addMouseMotionListener(JButton.this);
        then you can hold the mouse motion event by the reference of JButton.
        • It seems that your concept is not quite clear here
          1. The following toy code is OK:

          public class XXX extends JPanel implements MouseMotionListener{

          JButton jButton = new jButton("Test");
          jButton.addMouseMotionListener(this);

          public void mouseDrapped(MouseEvent e){
          //Do this
          }
          public void mouseMoved9MouseEvent e){
          //Do that
          }

          }
          Comments: "JButton.this" is at least a poor syntax.

          2. In Java gragramming, it is a common practice to have a Container be an EventListener. But it is not a good idea to let Button or JButton be a listener. Academically, it is OK.
        • We can use inherited methods only in the definition of the sub class.
          This is a flaw in Java. Once a subclass extends a superclass, there is
          NO WAY to use the methods of the super class. We can use inherited methods ONLY
          in the definition of the subclass. Please look at the following toy code.


          class Daddy{
          public void smoke(){
          System.out.println("Cigar! Cigar!");
          }
          }

          class Sonny extends Daddy{
          public void smoke(){
          //This calling of inherited methods is OK!
          super.smoke();
          System.out.println("Marlboro! Marlboro!!");
          }
          }

          public class Test{
          Sonny sonny = new Sonny();
          sonny.smoke();
          // The following code does not work!
          //sonny.super.smoke();
          }
          }