Swing JTextField
JTextField類是一個組件,它允許編輯一個單行文本。
類聲明
以下是聲明的 javax.swing.JTextField 類:
public class JTextField extends JTextComponent implements SwingConstants
字段域
以下是javax.swing.JList類字段域:
-
static String notifyAction -- 動作名稱的字段已被接受的內容發送通知。
類構造函數
S.N. | 構造函數 & 描述 |
---|---|
1 |
JTextField() Constructs a new TextField. |
2 |
JTextField(Document doc, String text, int columns) Constructs a new JTextField that uses the given text storage model and the given number of columns. |
3 |
JTextField(int columns) Constructs a new empty TextField with the specified number of columns. |
4 |
JTextField(String text) Constructs a new TextField initialized with the specified text. |
5 |
JTextField(String text, int columns) Constructs a new TextField initialized with the specified text and columns. |
類方法
S.N. | 方法 & 描述 |
---|---|
1 |
protected void actionPropertyChanged(Action action, String propertyName) Updates the textfield's state in response to property changes in associated action. |
2 |
void addActionListener(ActionListener l) Adds the specified action listener to receive action events from this textfield. |
3 |
protected void configurePropertiesFromAction(Action a) Sets the properties on this textfield to match those in the specified Action. |
4 |
protected PropertyChangeListener createActionPropertyChangeListener(Action a) Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties. |
5 |
protected Document createDefaultModel() Creates the default implementation of the model to be used at construction if one isn't explicitly given. |
6 |
protected void fireActionPerformed() Notifies all listeners that have registered interest for notification on this event type. |
7 |
AccessibleContext getAccessibleContext() Gets the AccessibleContext associated with this JTextField. |
8 |
Action getAction() Returns the currently set Action for this ActionEvent source, or null if no Action is set. |
9 |
ActionListener[] getActionListeners() Returns an array of all the ActionListeners added to this JTextField with addActionListener(). |
10 |
Action[] getActions() Fetches the command list for the editor. |
11 |
int getColumns() Returns the number of columns in this TextField. |
12 |
protected int getColumnWidth() Returns the column width. |
13 |
int getHorizontalAlignment() Returns the horizontal alignment of the text. |
14 |
BoundedRangeModel getHorizontalVisibility() Gets the visibility of the text field. |
15 |
Dimension getPreferredSize() Returns the preferred size Dimensions needed for this TextField. |
16 |
int getScrollOffset() Gets the scroll offset, in pixels. |
17 |
String getUIClassID() Gets the class ID for a UI. |
18 |
boolean isValidateRoot() Calls to revalidate that come from within the textfield itself will be handled by validating the textfield, unless the textfield is contained within a JViewport, in which case this returns false. |
19 |
protected String paramString() Returns a string representation of this JTextField. |
20 |
void postActionEvent() Processes action events occurring on this textfield by dispatching them to any registered ActionListener objects. |
21 |
void removeActionListener(ActionListener l) Removes the specified action listener so that it no longer receives action events from this textfield. |
22 |
void scrollRectToVisible(Rectangle r) Scrolls the field left or right. |
23 |
void setAction(Action a) Sets the Action for the ActionEvent source. |
24 |
void setActionCommand(String command) Sets the command string used for action events. |
25 |
void setColumns(int columns) Sets the number of columns in this TextField, and then invalidate the layout. |
26 |
void setDocument(Document doc) Associates the editor with a text document. |
27 |
void setFont(Font f) Sets the current font. |
28 |
void setHorizontalAlignment(int alignment) Sets the horizontal alignment of the text. |
29 |
void setScrollOffset(int scrollOffset) Sets the scroll offset, in pixels. |
方法繼承
這個類從以下類繼承的方法:
-
javax.swing.text.JTextComponent
-
javax.swing.JComponent
-
java.awt.Container
-
java.awt.Component
-
java.lang.Object
JTextField 例子
選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >
SwingControlDemo.javapackage com.yiibai.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showTextFieldDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing Examples"); 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("User ID: ", JLabel.RIGHT); JLabel passwordLabel = new JLabel("Password: ", JLabel.CENTER); final JTextField userText = new JTextField(6); final JPasswordField passwordText = new JPasswordField(6); JButton loginButton = new JButton("Login"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = "Username " + userText.getText(); data += ", Password: " + 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); } }
編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。
D:SWING>javac comyiibaiguiSwingControlDemo.java
如果冇有錯誤出現,這意味著編譯成功。使用下麵的命令來運行程序。
D:SWING>java com.yiibai.gui.SwingControlDemo
驗證下麵的輸出