java 鼠标事件Dragged和Moved 及java显示GIF在JLabel、JButton

本文介绍Java中实现鼠标拖动与移动事件的具体方法,包括如何为窗口添加监听器,以及如何在Swing控件上显示GIF图片并进行绘图操作。还涉及在JLabel和JButton上显示及绘制GIF图片的技术细节。

本来真不想写日志的,一直用doc写东西,想写完了给个百度文库连接,慢慢发现doc已经到12页,发现慢慢很能写文档了、、、

而且开始慢慢的喜欢上java了,因为java的每行代码都是自己敲上很有成就感 !

我相信java会让我着迷的,但心里一直放不下C++和.net,毕竟自己也学习了很多时间了!全才吧,哈哈!

正题:

1 、 java 鼠标事件Dragged和Moved

在WIN32 MFC 、或者.net中鼠标的moving和drag只要代码敲上就给力,但java中并不是的,需要给窗口添加addMouseMotionListener监听器,代码如下:

MouseAdapter mAdapter = new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
        System.out.println("mouseDragged");
    }
    public void mouseMoved(MouseEvent e) {
        System.out.println("mouseMoved FRAME");
    }
};
addMouseListener(mAdapter);
addMouseMotionListener(mAdapter);

2、在SWING控件上显示GIF图片并在gif上画画

Java中显示GIF,可以使用swing中的控件,并且可以重写控件的paint的方法重而改变控件的外观,并且支持GIF动态的显示,并可以在GIF上画画,实在是强大和方便啊!

在一个JLabel上显示GIF

JLabel jl = new JLabel("1111111111111");
jl.setIcon(new ImageIcon("D:/1.GIF"));
jl.setBounds(10, 570, 100, 100);
this.add(jl);

在JLable上显示的GIF上画画:

class SyxGifLabel extends JLabel {
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2D = (Graphics2D)g;
        g2D.setStroke(new BasicStroke(5.0f));
        g2D.drawRoundRect(5, 5, this.getWidth()-10,                          this.getHeight()-10, 10, 10);
    }
}
    SyxGifLabel sjl = new SyxGifLabel();
    sjl.setIcon(new ImageIcon("D:/1.GIF"));
    sjl.setBounds(130, 570, 100, 100);
    this.add(sjl);

在JButton上显示GIF图片:

JButton jbtn = new JButton("1111");
jbtn.setIcon(new ImageIcon("D:/1.GIF"));
jbtn.setBounds(230, 570, jbtn.getIcon().getIconWidth(), jbtn.getIcon().getIconHeight());
this.add(jbtn);

在JButton上显示的GIF上画画:

class SyxJBtn extends JButton {

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2D = (Graphics2D)g;
        g2D.setStroke(new BasicStroke(5.0f));
        g2D.drawRoundRect(5, 5, this.getWidth()-10,this.getHeight()-10, 10, 10);
    }
}

image
注意上面paint中调用父类的paint方法了,如果不调用就不能画出是个Button的样子,

当然你也可以重写 paintBorder, paintChildren, paintComponent其中的方法!

初学java希望有错的请大侠指正,谢谢!


转载于:https://www.cnblogs.com/syxchina/archive/2011/01/25/2197254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值