Java Interface Programming (6), java Interface Programming
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
You can use icons in JLable or any component inherited from AbstractButton. You can use any gif file you want to use. To open a file and obtain the image, you only need to create an ImageIcon object and pass the file name to it. Then you can use it in the program.
The following example shows how to add images to the JLabel and JButton, and set different images for each button status. The Code is as follows:
Public class Icons extends JFrame {private static Icon [] icons; private JButton jb, jb2 = new JButton ("Disable"); public Icons () {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. setroloverenabled (true); jb. setrolow.con (icons [2]); jb. setPressedIcon (icons [2]); jb. setDisabledIcon (icons [4]); jb. setToolTipText ("prompt"); add (jb); jb2.addActionListener (new ActionListener () {@ Overridepublic void actionreceivmed (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 ();}}
The execution result is as follows:
It can be seen that the buttons display different pictures when they are pressed, disabled, or float. In addition, the "tooltip" function is added to the buttons. When the mouse stays on the button, the text of the prompt will appear, which makes the button have a very good animation effect.
The constructor of many different Swing components accepts parameters of the Icon type. You can also use setIcon () to play back or modify the icons.