Java development notes (one hundred twenty-eight) Swing icon

Mentioned earlier, AWT can not provide direct control that displays an image, this is a short board is criticized, because you have come to define a new control by the programmers themselves, for beginners very friendly. This problem is also to get rid of Swing, Swing, but does not provide a single image view, but to use JLabel label control to display an image. JLabel the setText method to set the text on the label, and setIcon method used to set the icon on the label, according to two methods of calling the situation or not, the contents of the tag shown can be divided into the following three conditions:
1, only call setText method, setIcon method is not called, then the label display only text.
2, only call setIcon method, the setText method is not called, then the label display only images.
3, both calling setText method in turn calls setIcon method, then the label while displaying text and image, and the image on the left, text on the right that the left and right text.
However, the method of input parameters setIcon Icon icon type is not cached image BufferedImage described earlier, which means the image is converted to an icon cache type. Wherein the conversion process uses the ImageIcon image icon tool, since the tool Icon implements the interface, so it can be used as examples of input parameters setIcon method. By constructing the method ImageIcon, will be able to cache an object image into an icon, the specific conversion code as follows:

		// the picture file is read into the cache image 
		BufferedImage Image = ImageIO.read (new new File ( "E: /apple.png")); 
		ImageIcon icon = new new ImageIcon (Image); // create an icon

 

Of course, ImageIcon itself is a tool icons, so why go through the read image files cached image and shift it with both hands? Go directly to the specified file path is read. So based on the code image files to create an icon object becomes the following:

		ImageIcon icon = new ImageIcon ( "E: /apple.png"); // create a specified path icon

 

But what is the meaning of the string passed in the constructor count? Is the string must be a file path right? Obviously the way the string passed is not strict. Better to pass a URL address object, clearly and simply tells the compiler, input parameter constructor must be a legitimate file address, as expressed as the following code:

		URL url = new URL ( "file : /// E: /apple.png"); // create a local path URL object 
		ImageIcon icon = new ImageIcon (url) ; // create an icon for the specified URL

 

Notes that when building URL object, add a file path string prefix "file: ///", indicates that the string as a local file path. The sample code in addition to a local file, URL objects can be used to express a network file, simply complete http address of the network can pass in a file, then create an icon object from the network picture is as follows:

		// Create a network address URL object; URL url = new new ( "https://profile.csdnimg.cn/C/1/5/1_aqi00") URL 
		ImageIcon icon = new new ImageIcon (url); // create one from network icon picture

 

Several of the above construction method, it can be used to create the icon object. After acquiring the icon object, you can call setIcon method to display an image, but also calls setIconTextGap method to set the gap distance between the icons and text in the label controls.
Next, look at each shows a combination of the results of the image of the text on the label, first, only the displayed image is not displayed text, the label control method of calling code examples are as follows:

		ImageIcon icon = new ImageIcon ( "E : /apple.png"); // create an icon a specified path 
		label.setIcon (icon); // Settings icon label 
		label.setText (null); // set the label's text is empty, then do not display text

 

Above to run the tests, the pop-up window interface shown below, the visible image on the label be centered.


Then, display images and text preparation, and the method setIcon setText method shall be specified label control object is not empty, the calling code at this time is as follows:

		ImageIcon icon = new ImageIcon ( "E : /apple.png"); // create a specified path icon 
		label.setIcon (icon); // set the label icon (the icon to the left of the text) 
		label.setIconTextGap (10); // set the size of the gap between the icons and text 
		label.setText ( "this is an apple"); // set the label's text

 

Above to run the tests, the pop-up window interface shown below, the visible images and text are displayed out.


Again, not only the text display image, the calling code label control as follows:

		label.setIcon (null); // Setup tab icon is empty, then do not display images 
		label.setText ( "This is an apple"); // set the label's text

 

Above to run the tests, the pop-up window interface shown below, becomes visible JLabel control the regular text labels.

 



See more Java technology articles " Java Development Notes (order) List of chapters "

Guess you like

Origin www.cnblogs.com/pinlantu/p/11240944.html