Java development notes (one hundred twenty) AWT text label

Introduced in front of a simple use of AWT window and its panels, which show only the controls out of Button A, there are a lot of fun easy to use controls to be introduced. The first is the text label Label, the control used to display text for a tile, it does not fancy beating, is entirely makeup of text characters. However, even text, can also have a distinct personality, like calligraphy as horizontal can also be vertical, either written on white paper can also be written on red paper, but also in a special font to write as regular script , running script, cursive, official script and so on. These highlight the personality, you need to call the appropriate method to achieve a text label, Label The following is a description of a common method:
setText: set the text content.
setAlignment: Sets the alignment of the text inside. Label.CENTER is centered, Label.LEFT represents alignment to the left, Label.RIGHT represents alignment to the right.
setPreferredSize: recommended setting text label width and height.
setBackground: set the background color of the text labels.
setForeground: set up text labels foreground color, in fact, the text color.
setFont: Set the text font (including style and size).
It should describe the AWT to add color and font usage, not just label Label, many controls will be used colors and fonts. Colors used in the Color tool, and it comes in several colors Color class constants, including white Color.WHITE, gray Color.GRAY, black Color.BLACK, red Color.RED, pink Color.PINK, orange Color.ORANGE, yellow Color .YELLOW, green Color.GREEN, Rose Color.MAGENTA, blue Color.CYAN, blue Color.BLUE and so on. But the seven colors of the rainbow - red orange yellow green blue purple, AWT actually did not provide a purple color constants had to be calculated by the programmer to the purple. In the computer industry standard, the display device commonly used RGB color mode, it is red, green and blue for the three primary colors, the three primary colors from each of several superimposed together, they form a colorful fast-paced world. Unexpectedly the AWT Color tool has adopted other HSB color model, well known to the public so that had first RGB color values ​​are converted to HSB color values ​​for the job, the conversion process is RGBtoHSB method call, then call getHSBColor method HSB color value obtain color examples. RGB color values ​​from the color to the final instance is available, complete conversion code is as follows:

	// use the RGB color values obtained AWT Examples 
	Private the getColor static Color (Red int, int Green, Blue int) { 
		// the RGB color value into a color value HSB array 
		float [] hsbs = Color.RGBtoHSB (red , green, Blue, null); 
		// get AWT HSB color values using a color example 
		color = color Color.getHSBColor (HSBS [0], HSBS [. 1], HSBS [2]); 
		return color; 
	}

 

Thus, a new method defined getColor fill specific RGB values ​​to obtain a colorful Color object. For example a mixture of red and blue violet formed, then only can be obtained the following line of the purple color of examples:

				Color purple = getColor (255, 0, 255); // mixture became red and blue violet

As the uses Font Font tool which determines the shape and size of the text, the method of constructing the Font call, the second parameter writing character fonts, including the default normal body Font.PLAIN, italic Font.ITALIC, crude Font.BOLD body; the second parameter indicates the size of the text, the default value is 12, the greater the value the greater the text. For example, the following code were created in large numbers in italics and bold were two font objects:

				// Create a No. 30 size and font objects italic 
				Font italic_big = new Font ( "large italic", Font.ITALIC, 30); 
				// create a No. 20 size and bold font object 
				Font bold_middle = new font ( "medium bold", Font.BOLD, 20);

Followed by related methods text labels, look at the actual interface display effect. In the first intermediate position of the window to add text labels, ready to observe the effect of the change in the Label Text the control, sample code label panel to add the following:

		Label label = new Label ( "here to see text effects"); // Create a text label 
		label.setAlignment (Label.CENTER); // set the alignment of the text labels 
		label.setPreferredSize (new Dimension (300, 50 )); // set the width and height of the text label recommended 
		panel panelCenter = new panel (); // create a central panel 
		panelCenter.add (label); // add text labels on the center panel 
		frame.add (panelCenter, BorderLayout.CENTER); / / Add the center panel to the intermediate position of the window

Then look to the example set the background color of the label, the following code calls a text label setBackground method:

		Panel panelTop = new Panel (); // create a top panel 
		Button btn1 = new Button ( "yellow background"); // Create a button 
		btn1.addActionListener (new ActionListener () {// Register button to click listener 
			public void actionPerformed (ActionEvent e) {// click event occurs 
				label.setBackground (Color.YELLOW); // set the background color of the text labels 
			} 
		}); 
		panelTop.add (btn1); // Add button on the top panel

 

Run the above background color codes, the window interface shown below click the button, the background color of the label area becomes visible yellow.

Examples of the foreground color look set, code calls the following methods setForeground text label:

		Button btn2 = new Button ( "foreground red"); // Create a button 
		btn2.addActionListener (new ActionListener () {// Register button to click listener 
			public void actionPerformed (ActionEvent e) { // click event occurred 
				label .setForeground (Color.RED); // set the foreground color of text labels (i.e., text color) 
			} 
		}); 
		panelTop.add (btn2); // Add button on the top panel

 

Run the code above foreground color, click the button interface window shown below, tab text color to red visible inside.

AWT comes aside several colors, purple programmer to define instances, the background color and the text label to purple, calling code at this time is as follows:

		Button btn3 = new Button ( "purple background"); // Create a button 
		btn3.addActionListener (new ActionListener () {// Register button to click listener 
			public void actionPerformed (ActionEvent e) { // click event occurred 
				Color purple = getColor (255, 0, 255); // red becomes mixed with the blue violet 
				label.setBackground (purple); // set the label background purple 
			} 
		}); 
		panelTop.add (btn3); / / Add button on the top panel

 

Run the above background color codes, the window interface shown below click the button, the background color of the label area visible to purple.

Next to the tag set large italic text, code examples setFont method using the font set as follows:

		Button btn4 = new Button ( "large italic"); // Create a button 
		btn4.addActionListener (new ActionListener () {// Register button to click listener 
			public void actionPerformed (ActionEvent e) {// click event occurred 
				// Create a number 30 and the size of the font object italic 
				Font italic_big = new Font ( "large italic", Font.ITALIC, 30); 
				label.setFont (italic_big); // set the font and size of text label 
			} 
		} ); 
		panelTop.add (btn4); // Add button on the top panel

 

The above operation of setting the font code window interface click the button as shown below, a visible label text italic, while a lot of characters becomes larger.

No. finally set the label text to bold, code examples setFont method using the font set as follows:

		Button btn5 = new Button ( "Medium Bold"); // Create a button 
		btn5.addActionListener (new ActionListener () {// Register button to click listener 
			public void actionPerformed (ActionEvent e) { // happens click event 
				// create a No. 20 size and bold font object 
				Font bold_middle = new Font ( "medium bold", Font.BOLD, 20); 
				label.setFont (bold_middle); // set the font and text labels size 
			} 
		}); 
		panelTop.add (btn5); // Add button on the top panel

 

The above operation of setting the font code window interface click the button as shown below, a visible label bold text, but the text becomes too big.

In addition, setBackground, setForeground, setFont these three methods are not only used for the label Label, Button Button can call them anything, including controls with text, we support to set the background color, foreground color and font through these three methods.



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

Guess you like

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