Packagecom.swing;ImportJava.awt.Color;Importjava.awt.Component;ImportJava.awt.Container;ImportJava.awt.Graphics;ImportJavax.swing.Icon;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;Importjavax.swing.SwingConstants;Importjavax.swing.WindowConstants;/*** 1: The way to display text or hint information in swing is to use the label, which supports text strings and icons * Focus is the label contains a text string and an icon * 2: The label can display a line of read-only text, an image or text with an image, it does not produce any type of event * It's simple to display text and pictures, but you can use the label's attributes to specify the alignment of the text on the label * The focus is simply to display text and pictures * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * , and set the level of the label content to its way * * 4:swing above the icon can be placed on the buttons, labels, and other components, used to describe the purpose of the component * * 5:swing through the icon interface to achieve the creation of the chart, you can create a given icon size, color and other features * If you use the icon interface, you must implement the icon's three interfaces *@authorBiexiansheng **/ Public classDrawIconImplementsicon{//To implement the icon interface, you must implement the three methods of icon Private intWidth//declare the width of the icon Private intHeight//declare the length of the icon//Defining construction Methods PublicDrawIcon (intWidthintheight) { This. width=width; This. height=height; } @Override Public voidPainticon (Component C, Graphics G,intXinty) {//TODO auto-generated Method StubG.filloval (x,y,width,height); //The learning difficulties here are in the graphics section. } @Override Public intGeticonwidth () {//implementing the Geticonwidth () method//TODO auto-generated Method Stub return This. Width; } @Override Public intGeticonheight () {//implementing the Geticonheight () method//TODO auto-generated Method Stub return This. Height; } Public Static voidMain (string[] args) {DrawIcon icon=NewDrawIcon (150,150);//Initialize and pass values to the construction method//Create a label and set the text on the label right in the middle of the labelJLabel jl=NewJLabel ("Test", Icon,swingconstants.center); JFrame JF=NewJFrame ();//Create a jframe windowContainer Container=jf.getcontentpane ();//Convert a window to a containerContainer.setbackground (Color.Blue); Container.add (JL); //set the external properties of a containerJf.settitle ("Use of tags, create icons"); Jf.setvisible (true); Jf.setsize (340,400); Jf.setdefaultcloseoperation (Windowconstants.exit_on_close); } }
The results of the instance run are
Java learn the use of icons in swing chapter tag JLabel