JTextField
类是一个允许编辑单行文本的组件。
类声明
以下是javax.swing.JTextField
类的声明 -
public class JTextField
extends JTextComponent
implements SwingConstants
字段
以下是javax.swing.JList
类的字段 -
static String notifyAction
- 要发送已接受字段内容的通知的操作的名称。
类构造函数
编号 | 构造函数 | 描述 |
---|---|---|
1 | JTextField() |
构造一个新的TextField。 |
2 | JTextField(Document doc, String text, int columns) |
构造一个新的JTextField,它使用给定的文本存储模型和给定的列数。 |
3 | JTextField(int columns) |
构造具有指定列数的新空TextField。 |
4 | JTextField(String text) |
构造使用指定文本初始化的新TextField。 |
5 | JTextField(String text, int columns) |
构造一个使用指定文本和列初始化的新TextField。 |
类构造函数
编号 | 类方法 | 描述 |
---|---|---|
1 | protected void actionPropertyChanged(Action action, String propertyName) |
更新文本字段的状态以响应关联操作中的属性更改。 |
2 | void addActionListener(ActionListener l) |
添加指定的操作侦听器以从此文本字段接收操作事件。 |
3 | protected void configurePropertiesFromAction(Action a) |
设置此文本字段上的属性以匹配指定Action中的属性。 |
4 | protected PropertyChangeListener createActionPropertyChangeListener(Action a) |
创建并返回一个PropertyChangeListener ,它负责侦听指定Action的更改并更新相应的属性。 |
5 | protected Document createDefaultModel() |
如果没有明确给出,则创建要在构造中使用的模型的默认实现。 |
6 | protected void fireActionPerformed() |
通知所有已注册对此事件类型的通知感兴趣的监听器。 |
7 | AccessibleContext getAccessibleContext() |
获取与此JTextField 关联的AccessibleContext 。 |
8 | Action getAction() |
返回此ActionEvent 源的当前设置Action ,如果未设置Action,则返回null 。 |
9 | ActionListener[] getActionListeners() |
返回使用addActionListener() 添加到此JTextField 的所有ActionListener 的数组。 |
10 | Action[] getActions() |
获取编辑器的命令列表。 |
11 | int getColumns() |
返回此TextField 中的列数。 |
12 | protected int getColumnWidth() |
返回列宽。 |
13 | int getHorizontalAlignment() |
返回文本的水平对齐方式。 |
14 | BoundedRangeModel getHorizontalVisibility() |
获取文本字段的可见性。 |
15 | Dimension getPreferredSize() |
返回此TextField 所需的首选大小。 |
16 | int getScrollOffset() |
获取滚动偏移量(以像素为单位)。 |
17 | String getUIClassID() |
获取UI的类ID。 |
18 | boolean isValidateRoot() |
通过验证textfield 来处理来自textfield 本身内部的重新验证调用,除非textfield 包含在JViewport 中,否则返回false 。 |
19 | protected String paramString() |
返回此JTextField 的字符串表示形式。 |
20 | void postActionEvent() |
处理在此文本字段上发生的操作事件,方法是将它们分派给任何已注册的ActionListener 对象。 |
21 | void removeActionListener(ActionListener l) |
删除指定的操作侦听器,以便它不再从此文本字段接收操作事件。 |
22 | void scrollRectToVisible(Rectangle r) |
向左或向右滚动字段。 |
23 | void setAction(Action a) |
设置ActionEvent 源的Action 。 |
24 | void setActionCommand(String command) |
设置用于操作事件的命令字符串。 |
25 | void setColumns(int columns) |
设置此TextField 中的列数,使布局无效。 |
26 | void setDocument(Document doc) |
将编辑器与文本文档关联。 |
27 | void setFont(Font f) |
设置当前字体。 |
28 | void setHorizontalAlignment(int alignment) |
设置文本的水平对齐方式。 |
29 | void setScrollOffset(int scrollOffset) |
设置滚动偏移(以像素为单位)。 |
方法继承
该类继承以下类中的方法 -
javax.swing.text.JTextComponent
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
JTextField示例
使用编辑器创建以下Java程序 -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextFieldExample {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public JTextFieldExample(){
prepareGUI();
}
public static void main(String[] args){
JTextFieldExample swingControlDemo = new JTextFieldExample();
swingControlDemo.showTextFieldDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing JTextField示例");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showTextFieldDemo(){
headerLabel.setText("Control in action: JTextField");
JLabel namelabel= new JLabel("用户名: ", JLabel.RIGHT);
JLabel passwordLabel = new JLabel("密码: ", JLabel.CENTER);
final JTextField userText = new JTextField(6);
final JPasswordField passwordText = new JPasswordField(6);
JButton loginButton = new JButton("登录");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "用户名: " + userText.getText();
data += ", 密码: " + new String(passwordText.getPassword());
statusLabel.setText(data);
}
});
controlPanel.add(namelabel);
controlPanel.add(userText);
controlPanel.add(passwordLabel);
controlPanel.add(passwordText);
controlPanel.add(loginButton);
mainFrame.setVisible(true);
}
}
执行上面示例代码,得到以下结果: