SWING - JTextField 類



簡介

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)

設定該文字欄位上的屬性以匹配指定操作中的屬性

4

protected PropertyChangeListener createActionPropertyChangeListener(Action a)

建立一個 PropertyChangeListener,該監聽器負責偵聽指定操作的更改並更新適當的屬性

5

protected Document createDefaultModel()

如果未明確給出,則建立將用於構建的模型的預設實現

6

protected void fireActionPerformed()

通知對註冊的此事件型別感興趣的所有偵聽器

7

AccessibleContext getAccessibleContext()

獲取與此 JTextField 關聯的 AccessibleContext

8

Action getAction()

如果未設定任何操作,則返回為此 ActionEvent 源當前設定的操作,否則返回 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()

來自文字欄位自身的 revalidate 呼叫將透過驗證文字欄位來處理,除非文字欄位包含在 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)

設定滾動偏移量(以畫素為單位)。

Methods Inherited

此類從以下類繼承方法——

  • javax.swing.text.JTextComponent
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JTextField 示例

在您選擇的任何編輯器中建立一個 Java 程式,例如 D:/ > SWING > com > tutorialspoint > gui >

SwingControlDemo.java

package com.tutorialspoint.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 com\tutorialspoint\gui\SwingControlDemo.java

如果沒有錯誤,則表示編譯成功。使用以下命令執行程式。

D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

驗證以下輸出。

Swing JTextField
swing_controls.htm
廣告