Java Interface Programming (6), java Interface Programming

Source: Internet
Author: User

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.

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.