简体   繁体   中英

How to only allow capital letters in Java GUI?

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

JTextField jt = new JTextField(12);

Use a DocumentFilter

Take a look at Implementing a Document Filter for details and MDP's Weblog for examples

属于我们

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

}

Try the following:

 inputValue.toUpperCase();

This way you will capitalize everything before processing the data.

DocumentFilter

Do Like this example

 DocumentFilter dfilter = new UpcaseFilter();

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

One more example of 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);
  }
}

You create a JTexfield, then you need add the event keyTyped and add this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM