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