摘要:介紹是一個輕量級組件,它允許編輯單行文本。把字段文本用作針對的命令字符串。右對齊尾部對齊在所需的字段文本尺寸小于為它分配的尺寸時使用。這是由和方法確定的。如果具有操作偵聽器,則導致偵聽器獲取一個,并使用事件。
介紹
JTextField是一個輕量級組件,它允許編輯單行文本。
JTextField 具有建立字符串的方法,此字符串用作針對被激發(fā)的操作事件的命令字符串。java.awt.TextField 把字段文本用作針對 ActionEvent 的命令字符串。如果通過 setActionCommand 方法設(shè)置的命令字符串不為 null,則 JTextField 將使用該字符串來保持與 java.awt.TextField 的兼容性,否則將使用字段文本來保持兼容性。
setEchoChar 和 getEchoChar 方法不是直接提供的,以避免可插入的外觀的新實現(xiàn)意外公開密碼字符。為了提供類似密碼的服務(wù),多帶帶的類 JPasswordField 擴展了 JTextField,從而通過可插入外觀獨立地提供此服務(wù)。
JTextField 的水平對齊方式可以設(shè)置為左對齊、前端對齊、居中對齊、右對齊或尾部對齊。右對齊/尾部對齊在所需的字段文本尺寸小于為它分配的尺寸時使用。這是由 setHorizontalAlignment 和 getHorizontalAlignment 方法確定的。默認情況下為前端對齊。
文本字段如何使用 VK_ENTER 事件取決于文本字段是否具有任何操作偵聽器。如果具有操作偵聽器,則 VK_ENTER 導致偵聽器獲取一個 ActionEvent,并使用 VK_ENTER 事件。這與 AWT 文本字段處理 VK_ENTER 事件的方式是兼容的。如果文本字段沒有操作偵聽器,則從 1.3 版本開始不使用 VK_ENTER 事件。而是處理祖先組件的綁定,這將啟用 JFC/Swing 的默認按鈕特性。
Swing 不是線程安全的
構(gòu)造函數(shù)JTextField() 構(gòu)造一個新的 TextField
JTextField(Document doc, String text, int columns) 構(gòu)造一個新的 JTextField,它使用給定文本存儲模型和給定的列數(shù)。
JTextField(int columns) 構(gòu)造一個具有指定列數(shù)的新的空 TextField。
JTextField(String text) 構(gòu)造一個用指定文本初始化的新 TextField。
JTextField(String text, int columns) 構(gòu)造一個用指定文本和列初始化的新 TextField。
常用的函數(shù)get/setHorizontalAlignment(int alignment) 設(shè)置/得到文本的水平對齊方式。其中水平的對齊方式有:JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING (the default)
JTextField.TRAILING
setFont(Font font) 設(shè)置字體
setScrollOffset(int scrollOffset) 獲取滾動偏移量(以像素為單位)。
setDocument(Document doc) 將編輯器與一個文本文檔關(guān)聯(lián),這里的意思就是將此文本框與一個文本文檔關(guān)聯(lián),這將會保持內(nèi)容一致,如果一個改變了,另外一個也會改變。
setInputVerifier(verifier) 設(shè)置驗證方式,如果此文本不能通過驗證那么就不能將焦點聚焦到下一個組件上,就會一直聚焦到這個文本框上
setDragEnabled(boolean x) 設(shè)置在文本框中是否能夠拖放文本,為true則是能夠,這里的意思就是能夠?qū)⑽谋具x中后能不能將文本拖走
addActionListener(ActionListener action) 添加監(jiān)聽機制,輸入文本按回車即可觸發(fā),和按鈕的監(jiān)聽機制相同
write(InfileWriter writer) 將文本框中的內(nèi)容輸入到文件中
addKeyListener(KeyListener event) 添加鍵盤監(jiān)聽,在文本框中輸入內(nèi)容時會觸發(fā)鍵盤,其中有按下,釋放,鍵入的動作,詳情見官方文檔
一個簡單的實例import javax.swing.*;
import java.awt.*;
class text extends JFrame {
private JTextField textField1;
private JTextField textField2;
public static void main(String args[]) {
text my = new text();
my.setVisible(true);
}
public text() {
//this.setBounds(100,100,300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(2, 1));
textField1 = new JTextField(10);
textField2 = new JTextField();
panel.add(textField1);
panel.add(textField2);
this.getContentPane().add(panel, BorderLayout.CENTER);
this.pack();
InputVerifier verifier = new InputVerifier() { //添加驗證方式
@Override
public boolean verify(JComponent input) { //重載函數(shù)
boolean value;
textField1 = (JTextField) input; //將input組件強制轉(zhuǎn)化為JTextField類型的單行文本框
return textField1.getText().equals("pass"); //判斷是否輸入的時pass,如果不是就會驗證錯誤
}
};
textField1.setInputVerifier(verifier); //設(shè)置驗證方式
textField1.setHorizontalAlignment(JTextField.CENTER); //設(shè)置水平對齊方式
Font font = new Font("楷體", Font.BOLD + Font.ITALIC, 20);
textField1.setFont(font); //設(shè)置字體
textField1.setDragEnabled(true); //設(shè)置在單行文本框中能夠拖放文本,如果為false則不能夠拖放文本
}
}
關(guān)聯(lián)文本文檔
import java.awt.Container;
import java.awt.GridLayout;
/*from w ww.jav a 2s . co m*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;
public class Main extends JFrame {
JLabel nameLabel = new JLabel("Name:");
JLabel mirroredNameLabel = new JLabel("Mirrored:");
JTextField name = new JTextField(20);
JTextField mirroredName = new JTextField(20);
public Main() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(2, 0));
Container contentPane = this.getContentPane();
contentPane.add(nameLabel);
contentPane.add(name);
contentPane.add(mirroredNameLabel);
contentPane.add(mirroredName);
Document nameModel = name.getDocument(); //得到文本框的文本文檔,將之與第二個文本框關(guān)聯(lián)
mirroredName.setDocument(nameModel); //兩個文本框中的內(nèi)容相互關(guān)聯(lián),這樣只需要在一個里面輸入文本,同時也會在另外一個文本框中顯示
pack();
setVisible(true);
}
public static void main(String[] args) {
Main frame = new Main();
}
}
Action Listener(動作監(jiān)聽機制)說明:這里是將兩個文本框相關(guān)聯(lián),這樣就能達到一個文本框輸入的同時,另外一個也會同時更新內(nèi)容
輸入文本后按回車即可觸發(fā)
import java.awt.event.ActionEvent;
//from w w w. ja va2s .c o m
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField jTextField1 = new JTextField();
jTextField1.setText("jTextField1");
//添加監(jiān)聽機制
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("action");
}
});
frame.add(jTextField1);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
驗證文本內(nèi)容
使用InputVerifier)驗證
import java.awt.BorderLayout;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Verifier Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
InputVerifier verifier = new InputVerifier() { //創(chuàng)建一個驗證
public boolean verify(JComponent comp) {
boolean returnValue;
JTextField textField = (JTextField) comp; //強制轉(zhuǎn)換,將控件類型的comp轉(zhuǎn)換成JTextFiled類型的
try {
Integer.parseInt(textField.getText()); //將輸入的內(nèi)容轉(zhuǎn)化程int類型,如果輸入的字符串不是十進制的話就會觸發(fā) //NumberFormateException錯誤
returnValue = true;
} catch (NumberFormatException e) {
returnValue = false;
}
return returnValue; //如果返回false的話,那么指針就會一直聚焦在此文本框中,不能移動到其他的組件上
}
};
textField1.setInputVerifier(verifier);
frame.add(textField1, BorderLayout.NORTH);
frame.add(textField2, BorderLayout.CENTER);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
將文本框中的內(nèi)容保存到文件中說明:如果返回false的話,那么指針就會一直聚焦在此文本框中,不能移動到其他的組件上
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
class Main extends JFrame {
private JTextField textField;
private FileWriter writer;
public static void main(String args[]) {
Main my = new Main();
my.setVisible(true);
}
public Main() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("運行");
JLabel label = new JLabel("name");
textField = new JTextField();
panel.add(label, BorderLayout.WEST);
panel.add(textField, BorderLayout.CENTER);
String filename = "text.txt";
button.addActionListener(new ActionListener() { //添加一個按鈕觸發(fā)裝置,這里只要點擊一下anniu就會將文本框中的內(nèi)容輸入到文件中
@Override
public void actionPerformed(ActionEvent e) {
try {
writer = new FileWriter(filename, false); //創(chuàng)建一個寫入文件的對象,這里的false表示不在文件的末尾添加
textField.write(writer); //將單行文本中輸入的內(nèi)容寫入到文件中
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("false");
}
}
});
panel.add(button, BorderLayout.SOUTH);
this.getContentPane().add(panel, BorderLayout.CENTER);
this.pack();
}
}
復制、粘貼、剪切文本說明:這里使用的是FileWriter類將內(nèi)容寫入到文件中,詳情請看我的上一篇文章
這里使用的時copy()、paste()、cut()函數(shù)
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class Main {
public static void main(String args[]) {
final JTextField textField = new JTextField(15);
JButton buttonCut = new JButton("Cut");
JButton buttonPaste = new JButton("Paste");
JButton buttonCopy = new JButton("Copy");
JFrame jfrm = new JFrame("Cut, Copy, and Paste");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(230, 150);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
textField.cut();
}
});
buttonPaste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
textField.paste();
}
});
buttonCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent le) {
textField.copy();
}
});
textField.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent ce) {
System.out.println("All text: " + textField.getText());
if (textField.getSelectedText() != null)
System.out.println("Selected text: " + textField.getSelectedText());
else
System.out.println("Selected text: ");
}
});
jfrm.add(textField);
jfrm.add(buttonCut);
jfrm.add(buttonPaste);
jfrm.add(buttonCopy);
jfrm.setVisible(true);
}
}
添加鍵盤監(jiān)聽機制說明:這里使用的時用三個按鈕監(jiān)聽操作,只需要按住對應(yīng)的按鈕就會觸發(fā)機制
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Main extends JFrame {
public Main() throws HeadlessException {
setSize(200, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel usernameLabel = new JLabel("Username: ");
JTextField usernameTextField = new JTextField();
usernameTextField.setPreferredSize(new Dimension(100, 20));
add(usernameLabel);
add(usernameTextField);
usernameTextField.addKeyListener(new KeyAdapter() { //創(chuàng)建機制
public void keyReleased(KeyEvent e) { //重載函數(shù),釋放按鍵觸發(fā)
JTextField textField = (JTextField) e.getSource(); //得到最初發(fā)生event的組件對象,既是文本框?qū)ο? String text = textField.getText();
textField.setText(text.toUpperCase()); //將所有的小寫字母轉(zhuǎn)換成大寫字母
}
public void keyTyped(KeyEvent e) { //鍵入時觸發(fā)
}
public void keyPressed(KeyEvent e) { //釋放按鍵時觸發(fā)的函數(shù)
}
});
}
public static void main(String[] args) {
new Main().setVisible(true);
}
}
參考文檔
本人博客官方網(wǎng)站)
英文文檔
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66968.html
摘要:包括了圖形用戶界面器件如文本框,按鈕,分隔窗格和表。按照指定布局限制添加組件。移除指定位置的組件。通常文本框用于接收用戶信息或其他文本信息的輸入。因此,組件也稱為密碼文本框。創(chuàng)建一個具有出事文本信息以及制定列數(shù)的文本框。 Swing 是一個為Java設(shè)計的GUI工具包。 Swing是JAVA基礎(chǔ)類的一部分。 Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。 S...
摘要:事件對象攜帶了動作發(fā)生時的相關(guān)信息,比如通過事件對象獲取按鈕的字符串,通過字符串判斷后執(zhí)行不同的代碼。使用監(jiān)聽器的步驟自己創(chuàng)建一個類使用這個類創(chuàng)建一個對象,用按鈕對象的添加監(jiān)聽器方法添加這個對象。 ...
摘要:對于理論算法不再這累贅了。在查閱資料的時候發(fā)現(xiàn)算法不管用棧還是正則等等,似乎只處理操作符是的數(shù),這是很不可取的。所以需要先將中綴表達式轉(zhuǎn)化成后綴并標記多位數(shù)的操作符,然后在處理后綴表達式。 最后一次更新于2019/07/08 效果演示圖 showImg(https://segmentfault.com/img/bVbuIwj?w=388&h=290); 功能與流程 要制作一個簡易計算器...
摘要:文本域構(gòu)造方法摘要構(gòu)造新的。構(gòu)造顯示指定文本的新的。密碼框構(gòu)造方法摘要構(gòu)造一個新,使其具有默認文檔為的開始文本字符串和為的列寬度。登錄界面賬號密碼清除登錄觸發(fā)事件設(shè)置關(guān)閉方式,可以選擇多種關(guān)閉玄子選項 應(yīng)該最后一章了,前面有大神提到很少有人用Java做UI,這里就算是給像我這樣的初學者去了解窗體是怎么一回事的文章吧 文本框(JTextField) 構(gòu)造方法摘要 JTextField(...
閱讀 1181·2023-04-25 22:27
閱讀 945·2021-11-22 14:56
閱讀 1077·2021-11-11 16:54
閱讀 1804·2019-08-30 15:54
閱讀 3582·2019-08-30 13:20
閱讀 1276·2019-08-30 10:55
閱讀 2148·2019-08-26 13:34
閱讀 3345·2019-08-26 11:53