Java mouse events dragged and moved and Java display GIF in jlabel, jbutton

Source: Internet
Author: User

I really don't want to write logs. I have been using Doc to write something. I want to finish connecting to a Baidu Library. I slowly found that the doc has reached 12 pages, and I found that I can write documents ,,,

And I began to slowly like Java, because every line of Java code is very fulfilling!

I believe that Java will keep me fascinated, but I still cannot put C ++ and. Net in my mind. After all, I have learned a lot of time! All talent, haha!

Question:

1. Java mouse events dragged and moved

In Win32 MFC or. NET, the moving and drag of the mouse are powerful as long as the code is knocked on. But in Java, it is not necessary to add the addmousemotionlistener listener to the window. The Code is as follows:

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. Display GIF images on the swing control and draw images on the GIF.

To display GIF images in Java, you can use the control in swing, and rewrite the paint method of the control to re-change the control's appearance. It also supports dynamic GIF display and can draw images on the GIF, it is really powerful and convenient!

Display GIF on a jlabel:

Jlabel JL = new jlabel ("1111111111111 ");
Jl. seticon (New imageicon ("D:/1.gif "));
Jl. setbo UNDS (10,570,100,100 );
This. Add (jl );

Draw images on GIF displayed on jlable:

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 );

Display GIF images on jbutton:

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 );

Draw images on the GIF displayed on the jbutton:

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 );
}
}


Note that the painting method of the parent class is called in the paint. If it is not called, it cannot be shown as a button,

Of course, you can also rewrite the paintborder, paintchildren, and paintcomponent methods!

If you want to learn Java, correct it. Thank you!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.