In the graphical interface, text boxes and text areas are components used for information input and output.
Text box
The text box (JTextField) is a box in the interface used to input and output a line of text. The JTextField class is used to create text boxes. The interface related to the text box is ActionListener.
The basic contents of the text box handler include the following aspects:
Declare a text box name.
Create a text box object.
Add the text box object to a container.
Register a monitor for the text box object that needs to be controlled, and listen for the input end event of the text box (ie, enter the Enter key) event.
A method for processing text box events to complete the judgment and processing of intercepted events.
The main construction method of the JTextField class:
JTextField(), the character length of the text box is 1.
JTextField(int columns), the initial value of the text box is an empty string, and the character length of the text box is set to columns.
JTextField(String text), the initial value of the text box is the characters of text string.
JTextField(String text,int columns); The initial value of the text box is text, and the character length of the text box is columns.
Other methods of the JTextField class:
setFont(Font f), set the font
setText(String text), set the text in the text box
getText(), gets the text in the text box.
setEditable(boolean), specifies the editability of the text box, the default is true, editable.
setHorizontalAlignment(int alignment) sets the text alignment. Alignment methods are: JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT.
requestFocus(), set focus.
addActionListener(ActionListener), set the action monitor for the text box, and specify the ActionListener object to receive the input end action event that occurs on the text box.
removeActionListener(ActionListener) removes the text box monitor.
getColumns(), returns the number of columns of the text box.
getMinimumSize(), returns the minimum size required by the text box.
getMinimumSize(int), returns the minimum size required for the text box with the specified number of characters.
getPreferredSize(), returns the size that the text box hopes to have.
getPreferredSize(int), returns the desired size of the text box when the number of characters is specified.
[Example] The applet has two text boxes. One text box is used to enter an integer, and the other text box displays the square value of the integer. The program uses the string to basic type method Long.parseLong(text1.getText()) to read the string in the text box text1 and convert it into an integer. The program uses an instance of the Sqr class as a monitor, but in order for the monitor to access the variables of the main class, the variables in the main class are declared as class variables and no access permissions are set (view the source file).
The password box (JPasswordField) is a single-line input component, basically similar to JTextField. The password box has an additional shielding function, that is, when inputting, it will be output with a specified character (usually the * character). In addition to the text box methods introduced earlier, there are also some commonly used methods in password boxes:
getEchoChar(), which returns the echo character of the password.
setEchoChar(char), sets the echo character of the password box.
Text area
The text area (JTextArea) is an area in the form where text is placed. The main difference between a text area and a text box is that the text area can store multiple lines of text. The JTextArea class in the javax.swing package is used to create text areas. JTextArea component has no events.
The basic contents of the text area handler include the following aspects:
Declare a text area name.
Create a text area object.
Add the text area object to a container.
The main construction method of the JTextArea class:
JTextArea() creates a text area object with the default number of columns and rows.
JTextArea(String s), using s as the initial value, creates a text area object.
JTextArea(Strings, int x, int y), using s as the initial value, the number of rows as x, and the number of columns as y, creates a text area object.
JTextArea(int x, int y) creates a text area object with the number of rows as x and the number of columns as y.
Other common methods of the JTextArea class:
setText(String s), set the displayed text and clear the original text at the same time.
getText(), get the text of the text area.
insert(String s,int x), inserts the specified text at the specified position.
replace(String s, int x, int y), replace the text starting at the x position and ending at the y position with the given one.
append(String s), append text to the text area.
getCarePosition(), gets the position of the active cursor in the text area.
setCarePosition(int n), sets the position of the active cursor.
setLineWrap(boolean b), sets automatic line wrapping. By default, automatic line wrapping is not performed.
The following code creates a text area and sets it to automatically wrap.
JTextArea textA = new JTextArea(“我是一个文本区”,10,15); textA.setLineWrap(true);//设置自动换行
When there is a lot of content in the text area and cannot be displayed all in the text area, you can add scroll bars to the text area. To set the scroll bar for the text area, you can use the following code:
JTextArea ta = new JTextArea(); JScrollPane jsp = new JScrollPane(ta);//给文本区添加滚动条
在GUI中,常用文本框和文本区实现数据的输入和输出。如果采用文本区输入,通常另设一个数据输入完成按钮。当数据输入结束时,点击这个按钮。事件处理程序利用getText()方法从文本区中读取字符串信息。对于采用文本框作为输入的情况,最后输入的回车符可以激发输入完成事件,通常不用另设按钮。事件处理程序可以利用单词分析器分析出一个个数,再利用字符串转换数值方法,获得输入的数值。对于输出,程序先将数值转换成字符串,然后通过setText()方法将数据输出到文本框或文本区。
【例】小应用程序设置一个文本区、一个文本框和两个按钮。用户在文本区中输入整数序列,单击求和按钮,程序对文本区中的整数序列进行求和,并在文本框中输出和。单击第二个按钮,清除文本区和文本框中的内容。
import java.util.*;import java.applet.*;import java.awt.*; import javax.swing.*;import java.awt.event.*; public class J509 extends Applet implements ActionListener{ JTextArea textA;JTextField textF;JButton b1,b2; public void init(){ setSize(250,150); textA=new JTextArea("",5,10); textA.setBackground(Color.cyan); textF=new JTextField("",10); textF.setBackground(Color.pink); b1=new JButton("求 和"); b2=new JButton("重新开始"); textF.setEditable(false); b1.addActionListener(this); b2.addActionListener(this); add(textA); add(textF); add(b1);add(b2); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1){ String s=textA.getText(); StringTokenizer tokens=new StringTokenizer(s); //使用默认的分隔符集合:空格、换行、Tab符合回车作分隔符 int n=tokens.countTokens(),sum=0,i; for(i=0;i<=n-1;i++){ String temp=tokens.nextToken();//从文本区取下一个数据 sum+=Integer.parseInt(temp); } textF.setText(""+sum); } else if(e.getSource()==b2){ textA.setText(null); textF.setText(null); } } }
【例】小应用程序计算从起始整数到终止整数中是因子倍数的所有数。小程序容器用GridLayout布局将界面划分为3行列,第一行是标签,第二行和第三行是两个Panel。设计两个Panel容器类Panel1,Panel2,并分别用GridLayout布局划分。Panel1为1行6列,Panel2为1行4列。然后将标签和容器类Panel1,Panel2产生的组件加入到窗口的相应位置中。
import java.applet.*;import javax.swing.*; import java.awt.*;import java.awt.event.*; class Panel1 extends JPanel{ JTextField text1,text2,text3; Panel1(){//构造方法。当创建Panel对象时,Panel被初始化为有三个标签 //三个文本框,布局为GridLayout(1,6) text1=new JTextField(10);text2=new JTextField(10); text3=new JTextField(10);setLayout(new GridLayout(1,6)); add(new JLabel("起始数",JLabel.RIGHT));add(text1); add(new JLabel("终止数",JLabel.RIGHT));add(text2); add(new JLabel("因子",JLabel.RIGHT));add(text3); } } class Panel2 extends JPanel{//扩展Panel类 JTextArea text;JButton Button; Panel2(){//构造方法。当创建Panel对象时,Panel被初始化为有一个标签 //一个文本框,布局为GridLayout(1,4) text=new JTextArea(4,10);text.setLineWrap(true); JScrollPane jsp=new JScrollPane(text); Button=new JButton("开始计算"); setLayout(new GridLayout(1,4)); add(new JLabel("计算结果:",JLabel.RIGHT)); add(jsp); add(new Label());add(Button); } } public class J510 extends Applet implements ActionListener{ Panel1 panel1;Panel2 panel2; public void init(){ setLayout(new GridLayout(3,1)); setSize(400,200);panel1=new Panel1();panel2=new Panel2(); add(new JLabel("计算从起始数到终止数是因子倍数的数",JLabel.CENTER)); add(panel1);add(panel2); (panel2.Button).addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==(panel2.Button)){ long n1,n2,f,count=0; n1=Long.parseLong(panel1.text1.getText()); n2=Long.parseLong(panel1.text2.getText()); f=Long.parseLong(panel1.text3.getText()); for(long i=n1;i<=n2;i++){ if(i%f==0) panel2.text.append(String.valueOf(i)+""); } } } }
更多解析Java图形化编程中的文本框和文本区相关文章请关注PHP中文网!