The first thing to note is that the convenient display of images on components is one of swing's superior features than AWT.
If you know how to implement this method in AWT, please send me a letter.
In swing, buttons, lable, and other components can easily use images to change the appearance of a button.
This is usually the case when a button object is:
JButton button = new JButton ("Click me ");
In this way, a button object is created, and the text displayed above is Click me.
Now let's make some changes:
ImageIcon buttonIcon = new ImageIcon ("button.gif", "a background of button ");
JButton button = new JButton (buttonIcon );
These two lines of code achieve the purpose, and the image button.gif is displayed on the button. Note that we use the ImageIcon class, while
It is not an Image class. The second parameter in ImageIcon is the description of the ImageIcon object.
This is used by the dedicated Ive technology.
If you want to display text and images on the button at the same time, you can also do this:
JButton button = new JButton ("Click me", buttonIcon );
In addition, you can also define the position of an image on the component, such:
JButton button = new JButton ("Click me", buttonIcon, JButton. CENTER );