当前位置: 代码迷 >> Java相关 >> [原创]MouseListener的实践例子
  详细解决方案

[原创]MouseListener的实践例子

热度:348   发布时间:2007-07-29 09:31:43.0
[原创]MouseListener的实践例子

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: jdk2006 QQ:610540675
*/ 时间: 2007-7-29 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
MouseListener的使用:

MouseListener有五中方法:
public void mouseClicked(MouseEvent e);
public void mousePressed(MouseEvent e);
public void mouseReleased(MouseEvent e);
public void mouseEntered(MouseEvent e);
public void mouseExited(MouseEvent e);
下面举一个mouseClicked(MouseEvent e)方法的例子来说明其使用:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/*
*我们继承WindowAdapter抽象类,我们重定义mousePressed()方法
*/

public class PictrueDemo{
JFrame frame;
JPanel conPane;
JLabel label;
public void create(){
frame=new JFrame();
frame.setLocation(new Point(500,300));
conPane=(JPanel)frame.getContentPane();
conPane.setPreferredSize(new Dimension(200,200));
conPane.setLayout(new BorderLayout());
ImageIcon image=new ImageIcon(".\\6.jpg");
label=new JLabel(image);
//为label添加监听事件
label.addMouseListener(new simpleMouseAdapter());
conPane.add(label);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//simpleMouseAdapter
private class simpleMouseAdapter extends MouseAdapter{
public void mousePressed(MouseEvent e){
ImageIcon im=new ImageIcon(".\\9.jpg");
label.setIcon(im);
}
}
public static void main(String[] args){
PictrueDemo demo=new PictrueDemo();
demo.create();
}
}

搜索更多相关的解决方案: 例子  实践  

----------------解决方案--------------------------------------------------------