使用 a DocumentFilter
,只需将其添加到您的JTextField
喜欢中:
public class Test {
public void initComponents() {
//create frame
//add DoucmentFilter to JTextField
MyDocumentFilter myFilter = new MyDocumentFilter();
JTextField myArea = new JTextField();
((AbstractDocument)myArea.getDocument()).setDocumentFilter(myFilter);
//add components set frame visible
}
}
class MyDocumentFilter extends DocumentFilter {
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
super.replace(fb, i, i1, string, as);
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
}
或者
您可能想要创建一个自定义 JTextField,它已经具有DocumentFilter
(为了可重用性)类似:
public class MyCustomField extends JTextField {
public MyCustomField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return ((Document) new MyDocument());
}
static class MyDocument extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
super.insertString(fb, i, string, as);
}
@Override
public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
super.remove(fb, i, i1);
}
@Override
public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
super.replace(fb, i, i1, string, as);
}
}
}
从气垫船编辑
我更多地考虑这些方面
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.*;
public class Test {
public void initComponents() {
JPanel panel = new JPanel();
final MyDocumentFilter myFilter = new MyDocumentFilter();
final JTextField myArea = new JTextField(20);
((AbstractDocument) myArea.getDocument()).setDocumentFilter(myFilter);
panel.add(myArea);
panel.add(new JButton(new AbstractAction("Set Text") {
@Override
public void actionPerformed(ActionEvent arg0) {
myFilter.setFiltering(false);
myArea.setText("Fe Fi Fo Fum");
myFilter.setFiltering(true);
}
}));
JOptionPane.showMessageDialog(null, panel);
// add components set frame visible
}
public static void main(String[] args) {
new Test().initComponents();
}
}
class MyDocumentFilter extends DocumentFilter {
private boolean filtering = true;
@Override
public void replace(FilterBypass fb, int i, int i1, String string,
AttributeSet as) throws BadLocationException {
if (!filtering) {
super.replace(fb, i, i1, string, as);
}
}
@Override
public void remove(FilterBypass fb, int i, int i1)
throws BadLocationException {
int offset = 0;
int length = fb.getDocument().getLength();
super.remove(fb, offset, length);
}
@Override
public void insertString(FilterBypass fb, int i, String string,
AttributeSet as) throws BadLocationException {
if (!filtering) {
super.insertString(fb, i, string, as);
}
}
public void setFiltering(boolean filtering) {
this.filtering = filtering;
}
}