Java development notes (one hundred twenty-seven) Swing label

Lift the AWT label control Label, that can really makes a bad experience, not only do not support text wrapping, but also for the Chinese very friendly, both can display Chinese garbled, does not support the profound variety of Chinese fonts. Fortunately, an upgraded version of Swing JLabel label in all aspects optimized, making it a powerful label control, then in the end JLabel which enhance the place to have it? Judai author of 11 years.

First look at new ways of increasing the Label JLabel contrast, several of these methods are as follows:
setOpaque: Set the label is the background is opaque. true to opaque, false representation and transparent. Note If you want setBackground way to set the background color of the entry into force, it must call setOpaque method is set to true.
setHorizontalAlignment: Set label text alignment in the horizontal direction.
setVerticalAlignment: The label text is provided in a vertical alignment.
Since JLabel supports multiple lines of text displayed, so the internal text alignment is decomposed to two perpendicular horizontal directions, and each corresponding setHorizontalAlignment setVerticalAlignment, then no longer available setAlignment JLabel this direction alignment method is not distinguished.

Secondly, JLabel of setFont way to support Chinese characters, not as indifferent to the AWT Label. Originally the constructor Font Tools Font there are three parameters, the first parameter represents the font name and the second parameter indicates the font type, and the third parameter indicates the size of the text, however, for Label AWT's, the name of the font parameters did not role, regardless of fill italics or official script, it still shows the default Times New Roman. Today JLabel controls had literally enabled the foregoing Font Name parameter, as long as fill in "italics" or "official script" or other Chinese name of the font in the Font constructor, the interface will make text display the corresponding Chinese characters. For example, the following code fragment intend to show text in italics on the label control:

		JLabel label = new JLabel (); // JLabel without additional character encoding settings file 
		label.setPreferredSize (new Dimension (350, 100 )); // recommended settings tab width and height 
		label.setFont (new Font ( "italics" Font.PLAIN, 25)); // set the label text font and size 
		// set text labels. Note newline \ n Regardless 
		label.setText ( "Moonlight, suspected ground frost \ n raise my eyes to the moon, looking down and think of home.."); 
		Label.setHorizontalAlignment (JLabel.LEFT); // set the label text alignment in the horizontal direction 
		label.setVerticalAlignment (JLabel.CENTER); // set the label text alignment in the vertical direction 
		label.setOpaque (true); // set the label opaque 
		label.setBackground (Color.WHITE); // set the background color of the label

 

The above operation of test code, the pop-up window as shown below, visible characters on the screen and finally displayed as italics.


But according to the above interface effects, internal text labels still do not wrap, even add a line break in the string does not work. This is because the method setText JLabel HTML tag by wrapping operation is completed, if the label control requires long text strings wrap, the need to use html text string label wrapped. In other words: add "<html>" at the beginning of the string, add "</ html>" at the end of the string. Thus, it is possible to wrap the text read as follows this code to be set:

				// set the button's text (Wrap) 
				label.setText ( "<HTML> Moonlight, suspected ground frost lifting eyes to the moon, and think of home down </ html>..") ;

 

Running the modified test code, the pop-up window as shown below, visible text line would fit automatically start a new line, rather than just as ellipsis is replaced.


However, dependent on the wrap width of the control, only after the fill line, the remainder of the text will be wrapped to the next line. Sometimes we want the text string in a position to be line breaks, such as Tang song "Nostalgia", should have to change line at a full stop, it's only neat antithesis of the poem of. The reason why the line breaks in the setText method does not work, because the newline valid only in the case of plain text, it does not work on the page in HTML-formatted text, HTML text is truly effective dedicated line break "< br> ". The following is added in the middle of quatrains "<br>" code example:

				// set the text button (manual line feed) 
				label.setText ( "<HTML> Moonlight, suspected ground frost <br> raise eyes to the moon, and think of home down </ html>..") ;

 

Added Run line break test code, the pop-up window as shown below, which can be seen at the conclusion of the first Tang indeed advance wrap.


HTML tags not only to take the wraps, but also to personalize the style of the rich style. Originally setFont method can only be set to the current unified text font and size, the use of HTML tags can now set different styles of local text, such as <font> </ font> This tag supports color and text size settings, mark for <b> </ b> support to bold text, numerals <i> </ i> support text italic. HTML tags are followed by wrapping the end of each poem "Nostalgia", including red, yellow letters, bold, italic styles, and more specific examples are as follows demo code:

	// set the button's text (text color setting different segments) 
	label.setText ( "<HTML> <font Color = 'Red'> Moonlight, </ font> <b> suspected ground frost. </ b> <br> <font color = ' yellow'> lift eyes to the moon, </ font> <i> down think of home </ i> </ html> ").;

 

Running style demo code above, the pop-up window as shown below, visible pre-set text styles have presented it.

 


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

Guess you like

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