如何让 JTextField 只接受一个字母?
How to make JTextField to accept only one letter?
我有一个包含多个文本字段的应用程序我希望文本字段只接受一个字母并使用另一个字母。我正在尝试通过 ActionListener
和 KeyAdapter
并为所有文本字段只设置一个侦听器来做到这一点。
唯一的问题是,当它在一个文本字段中输入时,它不会在其他文本字段中输入。那么请问我的代码有什么问题?
到目前为止我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OnlyAlphabet extends JFrame
{
int checker=0;
private final KeyAdapter listener = new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if(c==KeyEvent.VK_BACK_SPACE )
{ checker =0 ; return; }
if(checker!=0)
{ e.consume();return;}
if(!(Character.isAlphabetic(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE))
{ e.consume(); return; }
if(Character.isAlphabetic(c) && checker==0)
{ checker =1; return; }
}
};
public void initComponent() {
setLayout(new FlowLayout());
JLabel lbl = new JLabel("Enter a Letter: ");
JTextField textField = new JTextField(15);
JTextField textField2 = new JTextField(15);
add(lbl);
add(textField);
add(textField2);
textField.addKeyListener(listener);
textField2.addKeyListener(listener);
setSize(300,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[]args){
new OnlyAlphabet().initComponent();
}
}
请检查一下,我想将相同的监听器添加到多个文本字段代码现在只包含 2 个,但会有更多像 25-30
当您想要过滤基础 Document
中更改的内容时,请使用 DocumentListener
when you want to be notified when text changes in the text filed. Use a DocumentFilter
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField[] fields = new JTextField[2];
for (int index = 0; index < fields.length; index++) {
fields[index] = createTextField();
add(fields[index], gbc);
}
}
protected JTextField createTextField() {
JTextField textField = new JTextField(4);
((AbstractDocument) textField.getDocument()).setDocumentFilter(new LimitDocumentFilter(1));
return textField;
}
}
public class LimitDocumentFilter extends DocumentFilter {
private int maxCharacters;
public LimitDocumentFilter(int maxChars) {
maxCharacters = maxChars;
}
protected String filter(FilterBypass fb, String str) {
StringBuilder buffer = new StringBuilder(str);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isAlphabetic(ch)) {
buffer.deleteCharAt(i);
}
}
int limit = maxCharacters - fb.getDocument().getLength();
if (limit == 0) {
return "";
}
return buffer.substring(0, Math.min(limit, buffer.length()));
}
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
String filtered = filter(fb, str);
if (!filtered.isEmpty()) {
super.insertString(fb, offs, filtered, a);
}
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {
String filtered = filter(fb, str);
if (!filtered.isEmpty()) {
super.replace(fb, offs, length, filtered, a);
}
}
}
}
我有一个包含多个文本字段的应用程序我希望文本字段只接受一个字母并使用另一个字母。我正在尝试通过 ActionListener
和 KeyAdapter
并为所有文本字段只设置一个侦听器来做到这一点。
唯一的问题是,当它在一个文本字段中输入时,它不会在其他文本字段中输入。那么请问我的代码有什么问题?
到目前为止我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OnlyAlphabet extends JFrame
{
int checker=0;
private final KeyAdapter listener = new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if(c==KeyEvent.VK_BACK_SPACE )
{ checker =0 ; return; }
if(checker!=0)
{ e.consume();return;}
if(!(Character.isAlphabetic(c) || (c==KeyEvent.VK_BACK_SPACE) || c==KeyEvent.VK_DELETE))
{ e.consume(); return; }
if(Character.isAlphabetic(c) && checker==0)
{ checker =1; return; }
}
};
public void initComponent() {
setLayout(new FlowLayout());
JLabel lbl = new JLabel("Enter a Letter: ");
JTextField textField = new JTextField(15);
JTextField textField2 = new JTextField(15);
add(lbl);
add(textField);
add(textField2);
textField.addKeyListener(listener);
textField2.addKeyListener(listener);
setSize(300,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[]args){
new OnlyAlphabet().initComponent();
}
}
请检查一下,我想将相同的监听器添加到多个文本字段代码现在只包含 2 个,但会有更多像 25-30
当您想要过滤基础 Document
DocumentListener
when you want to be notified when text changes in the text filed. Use a DocumentFilter
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(32, 32, 32, 32));
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
JTextField[] fields = new JTextField[2];
for (int index = 0; index < fields.length; index++) {
fields[index] = createTextField();
add(fields[index], gbc);
}
}
protected JTextField createTextField() {
JTextField textField = new JTextField(4);
((AbstractDocument) textField.getDocument()).setDocumentFilter(new LimitDocumentFilter(1));
return textField;
}
}
public class LimitDocumentFilter extends DocumentFilter {
private int maxCharacters;
public LimitDocumentFilter(int maxChars) {
maxCharacters = maxChars;
}
protected String filter(FilterBypass fb, String str) {
StringBuilder buffer = new StringBuilder(str);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isAlphabetic(ch)) {
buffer.deleteCharAt(i);
}
}
int limit = maxCharacters - fb.getDocument().getLength();
if (limit == 0) {
return "";
}
return buffer.substring(0, Math.min(limit, buffer.length()));
}
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
String filtered = filter(fb, str);
if (!filtered.isEmpty()) {
super.insertString(fb, offs, filtered, a);
}
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {
String filtered = filter(fb, str);
if (!filtered.isEmpty()) {
super.replace(fb, offs, length, filtered, a);
}
}
}
}