java 滑鼠事件Dragged和Moved 及java顯示GIF在JLabel、JButton

來源:互聯網
上載者:User

本來真不想寫日誌的,一直用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);
    }
}


注意上面paint中調用父類的paint方法了,如果不調用就不能畫出是個Button的樣子,

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

初學java希望有錯的請大俠指正,謝謝!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.