Java development notes (one hundred twenty-nine) Swing input box

Swing input box is still divided into two categories: single-line and multi-line input box input box, but compared to similar AWT controls, which have been changes in some details. First, let me single line input box, AWT single line input box named TextField, what character it will usually enter what character display, but once called setEchoChar method to set the echo character, TextField immediately become display only the ciphertext character. However embarrassing part is that, after setting the echo character, there is no way to cancel the original echo setting, input box object from silly to show only the ciphertext. Thus, the program code is difficult to determine whether an input box is displayed in plain text or cipher text, the encryption can not be determined whether or not the text input box. This will inevitably lead to confusion, assuming there are several types of controls are all TextField, the programmer turns out which is a text box, which is password box? Are you going to find that damn it setEchoChar method in the code a broad array?
Given the text box and password box has a different look, not suitable to express the same type, Swing therefore logical to split into two categories Talia controls: JTextField text box and password box JPasswordField. The former shows the characters as they are entered by the user, so as to cancel the echo character set method setEchoChar; the latter will be the default input character is displayed as a dot, of course, the programmer can also call setEchoChar resets the echo character. After such splitting process, regardless of the input plain text in Swing or password, you do not have to worry about the mistake. In addition, JTextField with the rest of JPasswordField essentially the same method, using them almost like the AWT TextField controls. For example, the following code example illustrates the process of calling a JTextField:

		JTextField textField = new JTextField (); // create a single line input box 
		textField.setEditable (true); // set the edit field allows 
		textField.setColumns (11); // set the input frame length of 11 characters 
		panel.add (textField); // add a single line input box on the panel

 

The operation of the above text box code, the pop-up window interface shown in FIG follows, showing a purely text input box.


Another example is the following code demonstrates the invocation of the password box JPasswordField:

		JPasswordField passwordField = new JPasswordField (); // create a password box 
		passwordField.setEditable (true); // set the password edit box allows 
		passwordField.setColumns (6); // set the length of the password box 11 characters 
		// passwordField. setEchoChar ( '*'); // set the echo character password box. The default character is echoed dot 
		panel.add (passwordField); // add a password box on the panel

 

The default operation of the echo character password box code above, following window interface shown in FIG seen password box is round and large black spots.


Then said multi-line input box, the AWT of the TextArea called multi-line input box, the control has a fault: a user input entered text, the line width does not exceed the wrap, the user must press the ENTER key to manually wrap. This design is undoubtedly very bad experience, you can not wrap it, knocking more user Enter key, it must complaining of. Fortunately, the Swing JTextArea control in a timely manner to make up for this problem, in addition to existing methods include the TextArea, JTextArea also adds text setLineWrap method used to set whether to allow each line, call the method its value is set to true, the next input Once over the width of each line will automatically wrap to the next line. Then adding a multi-line input method setLineWrap box calling code examples are as follows:

		JTextArea area = new JTextArea (); // create a multi-line input box 
		area.setEditable (true); // set the edit field allows 
		area.setColumns (14); // set the input frame length is 14 characters 
		area. setRows (3); // set the height of the input box character row 3 
		area.setLineWrap (true); // set whether to permit folding of each row. Is true, then the input characters exceeds the width of each line will automatically wrap 
		panel.add (area); // add multiple rows on a panel input box

 

The above operation of multi-line input box code, the pop-up window interface as shown in FIG, visible inside the text entry box is indeed the support wrap.


But compared with the AWT TextArea, Swing JTextArea is not displayed by default scroll bars, even if the total height of the text has exceeded the height of the input box, scroll bar Desired have not yet seen. This is because the scroll bar Swing also carry out alone, and gave it took a JScrollPane name is called, those who need to scroll up and down or left and right to scroll controls, would have to match the JScrollPane job. Scrollbar usage is very simple, just fill in the control object to be associated in the constructor, or by calling the scroll bar object setViewportView method, can be a scroll bar with the specified control bound together. Then added to the scroll bar object panel, so will complete the binding operation input block and the scroll bar, the specific binding code as follows:

		JTextArea area = new JTextArea (); // create a multi-line input box 
		area.setEditable (true); // set the edit field allows 
		area.setColumns (14); // set the input frame length is 14 characters 
		area. setRows (3); // set the height of the input box character row 3 
		area.setLineWrap (true); // if each line of the fold. Is true, then the input characters exceeds the width of each line will wrap 
		// because when the scroll bar is added below the JTextArea scroll bar is already associated, so here not necessary to add a separate multi-line input box 
		//panel.add(area); / / on the panel to add multi-line input box 
		JScrollPane scroll = new JScrollPane (area) ; // create a scroll bar 
		//scroll.setViewportView(area); // set the scroll bar associated control 
		panel.add (scroll); // add scroll bars on the panel

 

The above operation of the scroll bar binding code, as shown in the pop-up window interface, then fill several lines of text in the box, after the text exceeds the total height of the height control, the scroll bar on the right of the input box will appear approximately as a .

 



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

Guess you like

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