|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
public class JTextField
JTextField 是一个轻量级组件,它允许编辑单行文本。有关使用文本字段的信息和示例,请参阅 The Java Tutorial 中的 How to Use Text Fields 一节。
JTextField 应与 java.awt.TextField 具有源代码兼容性,理应如此。此组件具有 java.awt.TextField 类中没有的功能。有关其他功能,请参考超类。
JTextField 具有建立字符串的方法,此字符串用作针对被激发的操作事件的命令字符串。java.awt.TextField 把字段文本用作针对 ActionEvent 的命令字符串。如果通过 setActionCommand 方法设置的命令字符串不为 null,则 JTextField 将使用该字符串来保持与 java.awt.TextField 的兼容性,否则将使用字段文本来保持兼容性。
setEchoChar 和 getEchoChar 方法不是直接提供的,以避免可插入的外观的新实现意外公开密码字符。为了提供类似密码的服务,单独的类 JPasswordField 扩展了 JTextField,从而通过可插入外观独立地提供此服务。
通过添加 TextEvent 的 TextListener,可以监视 java.awt.TextField 的更改。在基于 JTextComponent 的组件中,通过 DocumentEvent 将更改从模型传播到 DocumentListeners。DocumentEvent 给出了更改的位置和更改种类(如果需要)。代码片段可能看起来如下所示:
DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);
JTextField 的水平对齐方式可以设置为左对齐、前端对齐、居中对齐、右对齐或尾部对齐。右对齐/尾部对齐在所需的字段文本尺寸小于为它分配的尺寸时使用。这是由 setHorizontalAlignment 和 getHorizontalAlignment 方法确定的。默认情况下为前端对齐。
文本字段如何使用 VK_ENTER 事件取决于文本字段是否具有任何操作侦听器。如果具有操作侦听器,则 VK_ENTER 导致侦听器获取一个 ActionEvent,并使用 VK_ENTER 事件。这与 AWT 文本字段处理 VK_ENTER 事件的方式是兼容的。如果文本字段没有操作侦听器,则从 1.3 版本开始不使用 VK_ENTER 事件。而是处理祖先组件的绑定,这将启用 JFC/Swing 的默认按钮特性。
通过对模型进行扩展和改变所提供的默认模型,可以很容易创建自定义字段。例如,以下代码片段将创建一个仅保存大写字符的字段。即使文本从剪贴板中粘贴过来或者通过编程方式而更改,此代码片段也是有效的。
public class UpperCaseField extends JTextField {
public UpperCaseField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
static class UpperCaseDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}
警告:Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy。
警告:此类的已序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder。
setActionCommand(java.lang.String),
JPasswordField,
addActionListener(java.awt.event.ActionListener)
| 嵌套类摘要 | |
|---|---|
protected class |
JTextField.AccessibleJTextField 此类实现对 JTextField 类的可访问性支持。 |
| 从类 javax.swing.text.JTextComponent 继承的嵌套类/接口 |
|---|
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding |
| 从类 javax.swing.JComponent 继承的嵌套类/接口 |
|---|
JComponent.AccessibleJComponent |
| 从类 java.awt.Container 继承的嵌套类/接口 |
|---|
Container.AccessibleAWTContainer |
| 从类 java.awt.Component 继承的嵌套类/接口 |
|---|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
| 字段摘要 | |
|---|---|
static String |
notifyAction 发送通知(已接收字段内容)的动作名称。 |
| 从类 javax.swing.text.JTextComponent 继承的字段 |
|---|
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY |
| 从类 javax.swing.JComponent 继承的字段 |
|---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
| 从类 java.awt.Component 继承的字段 |
|---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
| 从接口 javax.swing.SwingConstants 继承的字段 |
|---|
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST |
| 从接口 java.awt.image.ImageObserver 继承的字段 |
|---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
| 构造方法摘要 | |
|---|---|
JTextField() 构造一个新的 TextField。 |
|
JTextField(Document doc, String text, int columns) 构造一个新的 JTextField,它使用给定文本存储模型和给定的列数。 |
|
JTextField(int columns) 构造一个具有指定列数的新的空 TextField。 |
|
JTextField(String text) 构造一个用指定文本初始化的新 TextField。 |
|
JTextField(String text, int columns) 构造一个用指定文本和列初始化的新 TextField。 |
|
| 方法摘要 | |
|---|---|
protected void |
actionPropertyChanged(Action action, String propertyName) 更新文本字段的状态以响应关联动作中的属性更改。 |
void |
addActionListener(ActionListener l) 添加指定的操作侦听器以从此文本字段接收操作事件。 |
protected void |
configurePropertiesFromAction(Action a) 在此文本字段上设置属性,以匹配指定 Action 中的值。 |
protected PropertyChangeListener |
createActionPropertyChangeListener(Action a) 创建并返回一个负责侦听指定 Action 的更改以及更新适当属性的 PropertyChangeListener。 |
protected Document |
createDefaultModel() 如果没有显式给出构造时要使用的模型,则创建该模型的默认实现。 |
protected void |
fireActionPerformed() 通知对此事件类型需要的所有侦听器。 |
AccessibleContext |
getAccessibleContext() 获取与此 JTextField 关联的 AccessibleContext。 |
Action |
getAction() 返回此 ActionEvent 源当前设置的 Action,如果没有设置 Action 则返回 null。 |
ActionListener[] |
getActionListeners() 返回通过 addActionListener() 添加到此 JTextField 中的所有 ActionListener 的数组。 |
Action[] |
getActions() 获取编辑器的命令列表。 |
int |
getColumns() 返回此 TextField 中的列数。 |
protected int |
getColumnWidth() 返回列宽度。 |
int |
getHorizontalAlignment() 返回文本的水平对齐方式。 |
BoundedRangeModel |
getHorizontalVisibility() 获取文本字段的可见性。 |
Dimension |
getPreferredSize() 返回此 TextField 所需的首选大小 Dimensions。 |
int |
getScrollOffset() 获取滚动偏移量(以像素为单位)。 |
String |
getUIClassID() 获取 UI 的类 ID。 |
boolean |
isValidateRoot() 调用来自文本字段本身的 revalidate,将通过验证文本字段来处理,如果文本字段不包含在 JViewport 中,则在这种情况下将返回 false。 |
protected String |
paramString() 返回此 JTextField 的字符串表示形式。 |
void |
postActionEvent() 通过将其指派给所有已注册的 ActionListener 对象来处理发生在此文本字段上的操作事件。 |
void |
removeActionListener(ActionListener l) 移除指定的操作侦听器,以便不再从此文本字段接收操作事件。 |
void |
scrollRectToVisible(Rectangle r) 将字段向左或向右滚动。 |
void |
setAction(Action a) 设置 ActionEvent 源的 Action。 |
void |
setActionCommand(String command) 设置用于操作事件的命令字符串。 |
void |
setColumns(int columns) 设置此 TextField 中的列数,然后验证布局。 |
void |
setDocument(Document doc) 将编辑器与一个文本文档关联。 |
void |
setFont(Font f) 设置当前字体。 |
void |
setHorizontalAlignment(int alignment) 设置文本的水平对齐方式。 |
void |
setScrollOffset(int scrollOffset) 获取滚动偏移量(以像素为单位)。 |
| 从类 java.lang.Object 继承的方法 |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| 字段详细信息 |
|---|
public static final String notifyAction
| 构造方法详细信息 |
|---|
public JTextField()
TextField。创建一个默认的模型,初始字符串为
null,列数设置为 0。
public JTextField(String text)
TextField。创建列数为 0 的默认模型。
text - 要显示的文本,或者为
null
public JTextField(int columns)
TextField。创建默认的模型,初始字符串设置为
null。
columns - 用来计算首选宽度的列数;如果列设置为 0,则首选宽度将是组件实现的自然结果
public JTextField(String text,
int columns)
TextField。创建默认的模型。
text - 要显示的文本,或者为
null
columns - 用来计算首选宽度的列数;如果列被设置为 0,则首选宽度将是组件实现的自然结果
public JTextField(Document doc,
String text,
int columns)
JTextField,它使用给定文本存储模型和给定的列数。其他构造方法均调用此方法。如果文档为
null,则创建默认的模型。
doc - 要使用的文本存储;如果为
null,则通过调用
createDefaultModel 方法提供一个默认的存储
text - 要显示的初始文本,或者为
null
columns - 要用来计算首选宽度 >= 0 的列数;如果
columns 被设置为 0,则首选宽度将为组件实现的自然结果
IllegalArgumentException - 如果
columns < 0
| 方法详细信息 |
|---|
public String getUIClassID()
JComponent 中的
getUIClassID
JComponent.getUIClassID(),
UIDefaults.getUI(javax.swing.JComponent)
public void setDocument(Document doc)
JTextComponent 中的
setDocument
doc - 要显示/编辑的文档
JTextComponent.getDocument()
public boolean isValidateRoot()
revalidate,将通过验证文本字段来处理,如果文本字段不包含在
JViewport 中,则在这种情况下将返回 false。
JComponent 中的
isValidateRoot
JViewPort,则返回 false;否则返回 true
JComponent.revalidate(),
JComponent.isValidateRoot()
public int getHorizontalAlignment()
JTextField.LEFT JTextField.CENTER JTextField.RIGHT JTextField.LEADING JTextField.TRAILING
public void setHorizontalAlignment(int alignment)
JTextField.LEFT JTextField.CENTER JTextField.RIGHT JTextField.LEADING JTextField.TRAILING invalidate 和
repaint,并且激发
PropertyChange 事件("horizontalAlignment")。
alignment - 对齐方式
IllegalArgumentException - 如果
alignment 不是一个有效键
protected Document createDefaultModel()
PlainDocument 的一个实例。
public int getColumns()
TextField 中的列数。
public void setColumns(int columns)
TextField 中的列数,然后验证布局。
columns - 列数 >= 0
IllegalArgumentException - 如果
columns 小于 0
protected int getColumnWidth()
public Dimension getPreferredSize()
TextField 所需的首选大小
Dimensions。如果设置的列数为非 0 ,则将宽度设置为此数与列的宽度的乘积。
JComponent 中的
getPreferredSize
JComponent.setPreferredSize(java.awt.Dimension),
ComponentUI
public void setFont(Font f)
revalidate。
JComponent 中的
setFont
f - 新字体
Component.getFont()
public void addActionListener(ActionListener l)
l - 要添加的动作侦听器
public void removeActionListener(ActionListener l)
l - 要移除的操作侦听器
public ActionListener[] getActionListeners()
ActionListener 的数组。
ActionListener,或者不具有添加的侦听器时返回一个空数组
protected void fireActionPerformed()
EventListenerList
public void setActionCommand(String command)
command - 命令字符串
public void setAction(Action a)
ActionEvent 源的
Action。新的
Action 替换任何以前设置的
Action,而不影响通过
addActionListener 独立添加的
ActionListeners。如果
Action 已经是一个注册到
ActionEvent 源的
ActionListener,则不对它进行重新注册。
设置 Action 将导致立即更改支持 Action 的 Swing 组件中描述的所有属性。随后,文本字段的属性将随着 Action 属性的变化而自动更新。
此方法使用三个其他方法设置并帮助跟踪 Action 的属性值。它使用 configurePropertiesFromAction 方法立即更改文本字段的属性。要跟踪 Action 属性值中的更改,此方法应注册 createActionPropertyChangeListener 所返回的 PropertyChangeListener。当 Action 中的属性更改时,默认 PropertyChangeListener 调用 actionPropertyChanged 方法。
a -
JTextField 的
Action,或者为
null
Action,
getAction(),
configurePropertiesFromAction(javax.swing.Action),
createActionPropertyChangeListener(javax.swing.Action),
actionPropertyChanged(javax.swing.Action, java.lang.String)
public Action getAction()
ActionEvent 源当前设置的
Action,如果没有设置
Action 则返回
null。
ActionEvent 源的
Action,或者
null
Action,
setAction(javax.swing.Action)
protected void configurePropertiesFromAction(Action a)
Action 中的值。有关此方法设置的是哪些属性的更多信息,请参阅
支持 Action 的 Swing 组件。
a - 从其获取属性的
Action,或者为
null
Action,
setAction(javax.swing.Action)
protected void actionPropertyChanged(Action action,
String propertyName)
createActionPropertyChangeListener 所返回的
PropertyChangeListener 中调用此方法。子类通常不调用此方法。支持其他
Action 属性的子类应该重写此方法和
configurePropertiesFromAction。
有关此方法设置的属性列表,请参阅支持 Action 的 Swing 组件中的表。
action - 与此文本字段关联的
Action
propertyName - 已更改属性的名称
Action,
configurePropertiesFromAction(javax.swing.Action)
protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Action 的更改以及更新适当属性的
PropertyChangeListener。
警告:如果子类化此方法,则不要创建匿名的内部类。如果执行文本字段的生命周期,则它将被绑定到 Action 的值。
a - 文本字段的动作
Action,
setAction(javax.swing.Action)
public Action[] getActions()
JTextComponent 中的
getActions
public void postActionEvent()
ActionListener 对象来处理发生在此文本字段上的操作事件。通常,这通过以文本字段注册的控制器来调用。
public BoundedRangeModel getHorizontalVisibility()
字段的外观实现管理最小值、最大值,以及 BoundedRangeModel 的范围属性。
BoundedRangeModel
public int getScrollOffset()
public void setScrollOffset(int scrollOffset)
scrollOffset - 偏移量 >= 0
public void scrollRectToVisible(Rectangle r)
JComponent 中的
scrollRectToVisible
r - 要滚动的区域
JViewport
protected String paramString()
JTextField 的字符串表示形式。此方法仅在进行调试的时候使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为
null。
JTextComponent 中的
paramString
JTextField 的字符串表示形式
public AccessibleContext getAccessibleContext()
JTextField 关联的
AccessibleContext。对于
JTextFields 组件,
AccessibleContext 采用
AccessibleJTextField 的形式。必要时创建新的
AccessibleJTextField 实例。
Accessible 中的
getAccessibleContext
JTextComponent 中的
getAccessibleContext
AccessibleJTextField,它充当此
JTextField 的
AccessibleContext
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。