如何在Java GUI中只允许大写字母?

时间:2014-02-07 03:52:21

标签: java swing text jtextfield uppercase

我希望能够只向我的GUI输入大写字母,所以我只需要最简单的方法来限制/阻止人们输入小写字母。

JTextField jt = new JTextField(12);

5 个答案:

答案 0 :(得分:6)

使用DocumentFilter

请查看Implementing a Document Filter了解详情,MDP's Weblog查看示例

Belong to Us

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class UppercaseTest {

    public static void main(String[] args) {
        new UppercaseTest();
    }

    public UppercaseTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField field = new JTextField(20);
                ((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter() {

                    @Override
                    public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                        string = string.toUpperCase();
                        super.insertString(fb, offset, string, attr);
                    }

                    @Override
                    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                        text = text.toUpperCase();
                        super.replace(fb, offset, length, text, attrs);
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(field);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

答案 1 :(得分:3)

尝试以下方法:

 inputValue.toUpperCase();

这样,您可以在处理数据之前将所有内容都大写。

答案 2 :(得分:3)

DocumentFilter

喜欢这个例子

 DocumentFilter dfilter = new UpcaseFilter();

        JTextField jt = new JTextField();
        ((AbstractDocument) jt.getDocument()).setDocumentFilter(dfilter);

答案 3 :(得分:3)

DocumentFilter的另一个例子

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class Main extends JFrame {
  public Main() throws HeadlessException {
    setSize(200, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    DocumentFilter filter = new UppercaseDocumentFilter();

    JTextField firstName = new JTextField(10);
    ((AbstractDocument) firstName.getDocument()).setDocumentFilter(filter);

    JTextField lastName = new JTextField(10);
    ((AbstractDocument) lastName.getDocument()).setDocumentFilter(filter);

    add(firstName);
    add(lastName);
  }

  public static void main(String[] args) {
    new Main().setVisible(true);
  }

}

class UppercaseDocumentFilter extends DocumentFilter {
  public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,
      AttributeSet attr) throws BadLocationException {

    fb.insertString(offset, text.toUpperCase(), attr);
  }

  public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
      AttributeSet attrs) throws BadLocationException {

    fb.replace(offset, length, text.toUpperCase(), attrs);
  }
}

答案 4 :(得分:-1)

您创建了一个JTexfield,然后需要添加事件keyTyped并添加以下代码:

JTextField txtFirstName = new JTextField();
txtFirstName.addKeyListener(new KeyAdapter() {
    @Override
    public void keyTyped(KeyEvent arg0) {
        char charecter = arg0.getKeyChar();
            if (Character.isLowerCase(charecter)) {
                arg0.setKeyChar(Character.toUpperCase(charecter));
            }
        }
    });