我印象当中监听器一般都是 写成一个内部类(不知道是不是这么叫),直接写在需要监听的实例后面,但是也有其他的写法,
问:一般有几种写法,而它们有区别吗,区别在哪,谢谢

解决方案 »

  1.   

    简单的程序可以使用匿名内部类,要是为了维护方便可以是普通内部类。还可以让你的JFrame继承相应的接口。区别不大,主要是看怎么组织代码让别人容易阅读。通常使用的是写成普通内部类,使用Eclipse的WindowsBuilder插件可以帮助生成相应的代码。
      

  2.   

    目前为止就用过两种。t.addActionListener(new ActionListener(){
    });
    t.addActionListener(e);
    ActionLitener e=new ActionListener(){};
    区别嘛没感觉出来,感觉后者条理性强点吧
      

  3.   

    大家请看:
    scanDocContentPane[k].addMouseMotionListener(new MouseMotionListener(){
    @Override
      public void mouseDragged(MouseEvent e) {
      // TODO Auto-generated method stub
     }
    @Override
      public void mouseMoved(MouseEvent e) {
      // TODO Auto-generated method stub
       setPreferredSize(new Dimension(260,280));
         try {
    Thread.sleep(3000);
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();

    setPreferredSize(new Dimension(240,270));
    }});
    两个问题:1,为什么一定要写那些用不着的监听(@Override下面的)
              2,那个Panel是显示图片的,我想达到这样的效果,鼠标进入Panel范围内后,Panel稍微扩大,过3秒钟变回原样,不过好像不能实现
      

  4.   

    感觉上setPreferredSize(new Dimension(260,280));并不能改变相应的scanDocContentPane[k]的大小,不知道是为什么
      

  5.   

    Famous global IT company 上海
    Position: Senior Java Software Engineer
    Business Unit: Famous global IT company - BAS - China - ITSEResponsibilities
     Lead the development of complex software product and mobile applications. 
     Design layered application, including user interface, business functionality, and database access. 
     Work with other engineers, managers, product managers, QA, and operation teams to develop innovative solutions that meet et needs with respect to functionality, performance, scalability, and reliability while meeting realistic implementation schedules and adhering to development goals and principles. 
     Estimate engineering efforts, plan implementations, and rollout system changes. 
     Share release management duties during feature rollouts and share on-call responsibilities. 
     Developing, unit testing, maintenance and release of software products under Agile patterns.
     Experience in developing highly scalable applications a major plus. 
    Job Requirements 
     BS/BA in CS or related field. 
     5+ years experience in requirements analysis, design, coding and testing of scalable, distributed, fault-tolerant applications.
     Expertise required in object-oriented design methodology and enterprise application development in Java . 
     Hands-on experience with J2EE/J2SE application. 
     Hands-on experience in Agile projects.
     Good at OO methodology, and familiar with Java design patterns.
     Proven result-oriented person with a delivery focus in a fast pace, high quality environment.
     Self-motivated and work under pressure.
     Solid capabilities of self study for new technologies.
     Knowledge of software product development is a major plus
     Fluent English in both speaking and writing.有意者加MSN:[email protected]
      

  6.   

    三种,比如鼠标点击画一个圆,对鼠标的监听
    1,匿名类 写法
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    public class Test {
    public static void main(String args[]){
    Circle c1=new Circle();
    }}
    class Circle extends JFrame{
    int x,y;
    public Circle(){
    init();

    this.setBounds(50,50,700,700);
    this.setVisible(true);
    }
    public void paint(Graphics g){
    g.fillOval(x,y,3,3);
    }
    void init(){
    this.addMouseListener(new MouseAdapter(){//匿名类写法
    public void mouseClicked(MouseEvent e){
    x=e.getPoint().x;
    y=e.getPoint().y;
    repaint();
    }
    });
    }
    }
    2。内部类写法
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    public class Test {
    public static void main(String args[]){
    Circle c1=new Circle();
    }}
    class Circle extends JFrame{
    int x,y;
    public Circle(){
    init();

    this.setBounds(50,50,700,700);
    this.setVisible(true);
    }
    public void paint(Graphics g){
    g.fillOval(x,y,30,30);
    }
    void init(){
    this.addMouseListener(new Mo());//对内部类的实例化 }
    class Mo extends MouseAdapter{   //内部类
    public void mouseClicked(MouseEvent e){
    x=e.getPoint().x;
    y=e.getPoint().y;
    repaint();
    }
    }
    }
    3.外部类写法
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    public class Test {
    public static void main(String args[]){
    Circle c1=new Circle();
    }}
    class Circle extends JFrame{
    static int x,y;
    public Circle(){
    init();

    this.setBounds(50,50,700,700);
    this.setVisible(true);
    }
    public void paint(Graphics g){
    g.fillOval(x,y,30,30);
    }
    void init(){
    this.addMouseListener(new Mo(this)); //传递JFrame } }
    class Mo extends MouseAdapter{        //外部类
    JFrame f;
    public Mo(JFrame f){
    this.f=f;
    }
    public void mouseClicked(MouseEvent e){
    Circle.x=e.getPoint().x;
    Circle.y=e.getPoint().y;
    f.repaint();   //强制重画
    }
    }
      

  7.   


      for(h = 1;h < 15;h++){
    scanDocContentPane[h].addMouseListener(new MouseListener(){
    @Override
    public void mouseClicked(MouseEvent e) {
      System.out.println("ASDS"+h);
    TeleAScanPopFrame popFrame = new TeleAScanPopFrame();               popFrame.showPopFrame(scanDocContentPane[h].getImagePath());
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void mousePressed(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    });  
    }
    这样的监听结果,竟然是输出ASDS15,为什么不是点击中哪个Panel显示哪个?比如点击scanDocContentPane[4]应该输出SADS4才对吧