How to only allow capital letters in Java GUI?

让人想犯罪 __ 提交于 2019-12-23 20:03:06

问题


I want to be able to input only capital letters to my GUI and so I just need the easiest way to cap/stop people from entering lowercase.

JTextField jt = new JTextField(12);

回答1:


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

}



回答2:


Try the following:

 inputValue.toUpperCase();

This way you will capitalize everything before processing the data.




回答3:


DocumentFilter

Do Like this example

 DocumentFilter dfilter = new UpcaseFilter();

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



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/21618998/how-to-only-allow-capital-letters-in-java-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!