如何让 JTextField 只接受一个字母?

发布于 2025-01-17 14:38:47 字数 1418 浏览 3 评论 0原文

我有一个具有多个文本字段的应用程序,我希望文本字段仅采用一个字母并使用另一个字母。我试图通过 ActionListenerKeyAdapter 并为所有文本字段仅创建一个侦听器来完成此操作。

唯一的问题是,当它在一个文本字段中接受输入时,它不会在其他文本字段中接受输入。那么请问我的代码有什么问题吗?

到目前为止我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洛阳烟雨空心柳 2025-01-24 14:38:47

当您希望在文本字段中的文本发生更改时收到通知。当您想要过滤底层文档中发生更改的内容

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);
            }
        }

    }
}

Use a DocumentListener when you want to be notified when text changes in the text filed. Use a DocumentFilter when you want to filter what gets changed in the underlying Document

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);
            }
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文