如何让 JTextField 只接受一个字母?
我有一个具有多个文本字段的应用程序,我希望文本字段仅采用一个字母并使用另一个字母。我试图通过 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
I have an application with multiple text fields I want the text fields to take only one Letter and consume the other. I am trying to do it through ActionListener
and KeyAdapter
and by making only one listener for all text fields.
The only problem is when it takes input in one text field it does not take input in others. So please what's wrong with my code?
My code so far:
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();
}
}
Please check it out and I want to add the same Listener to multiple Textfields the code only contains 2 now but there will be more like 25-30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您希望在文本字段中的文本发生更改时收到通知。当您想要过滤底层文档中发生更改的内容
Use a
DocumentListener
when you want to be notified when text changes in the text filed. Use aDocumentFilter
when you want to filter what gets changed in the underlyingDocument