Java Interface Programming (6)------icons

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020

You can use icon in jlable or any component that inherits from AbstractButton. You can use any GIF file you want, to open a file and get a graphic, just create a ImageIcon object and pass the file name to it. Then you can use it in your program.

The following example adds a picture to JLabel and JButton, and sets a different picture for each state of the button, with the following code:

public class Icons extends JFrame {private static icon[] icons;private JButton jb,jb2 = new JButton ("Disable");p ublic Icon S () {setlayout (New FlowLayout ()); setvisible (true); SetSize (400,200); Settitle ("Icon");//TODO auto-generated Constructor stubicons = new Icon[]{new ImageIcon (GetClass (). GetResource ("Icon1.png")), New ImageIcon (GetClass (). GetResource ("Icon2.png")), New ImageIcon (GetClass (). GetResource ("Icon3.png")), New ImageIcon (GetClass (). GetResource ("Icon4.png")), New ImageIcon (GetClass (). GetResource ("Icon5.png")), New ImageIcon (GetClass (). GetResource ("Icon6.png"))};JB = new JButton (icons[3]); Add (new JLabel (icons[5])); jb.setrolloverenabled (true); Jb.setrollovericon (icons[2]); Jb.setpressedicon (icons[2]); Jb.setdisabledicon (Icons[4]); Jb.settooltiptext ("hint"); Add (JB); Jb2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO Auto-generated Method Stubif (jb.isenabled ()) {jb.setenabled (false); Jb2.settext ("Enable");} Else{jb.setenabled (true); Jb2.settext("Disable");}}); Add (JB2);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew Icons ();}}
execute the procedure with the following results:


   It can be seen that the button in the press, prohibit, float the display of the picture is different, and also to the button added a "ToolTip" function, when the mouse rests on the button, the text will appear prompt, which makes the button has a pretty good animation effect.

   The constructors for many different swing components accept the icon type parameter, or you can use SetIcon () to go back and forth or modify the icon.

Java Interface Programming (6)------icons

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.