一个类test,里面有一张图片
另外一个类mouseMotion,里面是关于鼠标的事件,点击图片要它响应鼠标该怎么写呢?
///////////////////////////////////////////////类test
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.image.*; 
import java.awt.color.*; 
import java.util.*;
import java.io.*;
public class test extends JFrame {
  private Panel panel1 = new Panel();
  private JScrollPane jScrollPane1 = new JScrollPane();
  private JLabel jLabel1 = new JLabel();
  private GridLayout gridLayout2 = new GridLayout();
  private Image image;
  private ImageIcon imageicon;
  private BufferedImage bimage;  public test() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {
    panel1.setBounds(new Rectangle(127, 43, 555, 600));
    panel1.setLayout(gridLayout2);
    this.getContentPane().add(panel1, null);
    panel1.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jLabel1, null);
    
    this.setSize(700,700);
    this.setVisible(true);
    mouseMotion  mm=new mouseMotion(panel1);
  }  public static void main(String [] args){
   test t=new test();
  }
}///////////////////////////////////////////////类mouseMotion
class mouseMotion extends MouseAdapter{
    Panel pan;
    public mouseMotion(Panel p){
        this.pan=p;
        p.addMouseListener(this);
    }    public void mouseClicked(MouseEvent e){
       int x=e.getX();
       int y=e.getY();
       System.out.println(x);
       System.out.println(y);    }
}
为什么点了图片上什么反应都没有呢???

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.image.*;
    import java.awt.color.*;
    import java.util.*;
    import java.io.*;public class test extends JFrame {
    private Panel panel1 = new Panel(); private JScrollPane jScrollPane1 = new JScrollPane(); private JLabel jLabel1 = new JLabel("Label"); private GridLayout gridLayout2 = new GridLayout(); /*
     * private Image image; private ImageIcon imageicon; private BufferedImage
     * bimage;
     */ public test() {
    try {
    jbInit();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } private void jbInit() throws Exception {
    panel1.setBounds(new Rectangle(127, 43, 555, 600));
    panel1.setLayout(gridLayout2);
    this.getContentPane().add(panel1, null);
    panel1.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jLabel1, null); this.setSize(700, 700);
    this.setVisible(true);
    // mouseMotion mm = new mouseMotion(panel1);

    jLabel1.addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    int x = e.getX();
    int y = e.getY();
    System.out.println(x);
    System.out.println(y);
    } public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

    } public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    } public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

    } public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

    }});
    } public static void main(String[] args) {
    test t = new test();
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }
      

  2.   

    因为你的Panel加了Label, 每次你点击其实没有点到panel上, 而是点在Label上, 把Label加上鼠标监听就行了.