1 回答
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
要從JTextField 中消除 Tab-Away 功能,您需要將其setFocusTraversalKeysEnabled屬性設(shè)置為false。完成此操作后,無(wú)法通過(guò)按 TAB(或 SHIFT-TAB)鍵從 JTextField 丟失焦點(diǎn),并且可以在 JTextField 的KeyPressed事件中檢測(cè)到按下 TAB 鍵的事實(shí)。
從 JTextField 中刪除 Backspace/Delete 功能的最簡(jiǎn)單方法是使用自定義DocumentFilter。通過(guò)使用空方法覆蓋過(guò)濾器的remove()方法,可以有效地消除退格鍵或刪除鍵功能。
這一切都可以通過(guò)使用我在下面提供的單一方法來(lái)完成。該方法允許您從提供的 JTextField 禁用或啟用 Tab 和 Backspace 功能:
/**
* Disables (and can again Enable) the TAB (or SHIFT-TAB), BACKSPACE, and DELETE keys when
* used within the supplied JTextField.<br><br>
*
* When the Tab key or Backspace key is hit then it can be detected within the
* JTextField's KeyPressed Event by way of:<pre>
*
* if (event.getKeyCode() == KeyEvent.VK_TAB) {
* System.out.println("TAB Key Pressed!");
* }
* else if (event.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
* System.out.println("BACKSPACE Key Pressed!");
* }</pre>
*
* @param jtextfield (JTextField) The desired JTextField variable name to
* control.<br>
*
* @param ON_OFF (Optional - Boolean - Default is true) If true (default) then
* Tab and Backspace is not allowed within the supplied JTextField. If false is
* supplied then Tab and Backspace is allowed within the supplied JTextField.
*/
public void noTABorBACKSPACE(JTextField jtextfield, boolean... ON_OFF) {
boolean on = true; // Default ON - No Tab Away and No Backspace allowed.
if (ON_OFF.length > 0) {
on = ON_OFF[0];
}
if (on) {
// Remove the TAB Away feature from the JTextField.
jtextfield.setFocusTraversalKeysEnabled(!on);
// Disable the Backspace feature from the JTextField.
// This is done with a custom Document Filter.
((AbstractDocument) jtextfield.getDocument()).setDocumentFilter(
new DocumentFilter(){
@Override
// By overriding the remove() method with an empty remove()
// method we effectively eliminate Backspace capabilities.
public void remove(DocumentFilter.FilterBypass fb, int i, int i1)
throws BadLocationException { }
}
);
}
else {
// Re-enable the TAB Away feature for the JTextField.
jtextfield.setFocusTraversalKeysEnabled(!on);
// Re-enable the Backspace feature for the JTextField.
// This is done by removing our custom Document Filter.
((AbstractDocument) jtextfield.getDocument()).setDocumentFilter(null);
}
}
如何使用此方法:
// To disable TAB and BACKSPACE
noTABorBACKSPACE(jTextField1);
// OR
// noTABorBACKSPACE(jTextField1, true);
// To re-enable TAB and BACKSPACE
noTABorBACKSPACE(jTextField1, false);
雖然 TAB 和 BACKSPACE 功能在提供的 JTextField 中被禁用,但您可以確定是否通過(guò) JTextField 的KeyPressed事件按下了這些特定鍵,例如:
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_TAB) {
System.out.println("TAB Key Hit!");
}
else if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
System.out.println("BACKSPACE Key Hit!");
}
}
您會(huì)注意到,當(dāng) JTextField 的setFocusTraversalKeysEnabled屬性設(shè)置為布爾值true 時(shí),您將無(wú)法檢測(cè)到 TAB 鍵何時(shí)被擊中,這是因?yàn)?TAB 鍵始終由KeyboardFocusManager 使用。當(dāng)setFocusTraversalKeysEnabled屬性設(shè)置為 boolean false時(shí),情況并非如此。
到目前為止,提供的代碼提供了刪除 Tab-Away 和 Backspace/Delete 功能的方法,但也許您希望保持Delete鍵處于活動(dòng)狀態(tài),而只刪除 TAB 和 BACKSPACE 鍵的功能。如果是這種情況,那么您可以通過(guò)使用 BACKSPACE 按鍵在 JTextField 的KeyPressed事件中執(zhí)行此操作:
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_TAB) {
System.out.println("TAB Key Hit!");
}
else if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
System.out.println("BACKSPACE Key Hit!");
evt.consume(); // Consume the BACKSPACE Key Press.
}
}
要阻止 TAB 鍵移動(dòng)焦點(diǎn),您仍然需要將setFocusTraversalKeysEnabled屬性設(shè)置為false。
添加回答
舉報(bào)
