Java 如何实现只接受数字和有限字符的JFormattextfield
对于变量金额和账号,我有两个Java 如何实现只接受数字和有限字符的JFormattextfield,java,swing,Java,Swing,对于变量金额和账号,我有两个JFormattedTextField。 目的: 两个字段只能接受数字 Acc No最多可包含15个字符,字符数从8到15不等。类似地,数量最多可以有6个字符,并且也会有所不同 为了实现这一点,我使用了MaskFormatter,但问题在于“变体”。有些acc是15位数字,有些是12位数字,因此当使用MaskFormatter限制为15位时,必须输入15位数字,否则当我们离开JFormattedTextField 在java swing中有什么方法可以同时实现这两种场
JFormattedTextField
。目的:
MaskFormatter
,但问题在于“变体”。有些acc是15位数字,有些是12位数字,因此当使用MaskFormatter
限制为15位时,必须输入15位数字,否则当我们离开JFormattedTextField
在java swing中有什么方法可以同时实现这两种场景吗?请建议我使用
文档过滤器。然后,您可以根据自己的具体需求定制过滤器
让您开始学习的一个基本示例:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class DigitFilter extends DocumentFilter
{
@Override
public void insertString(FilterBypass fb, int offset, String text, AttributeSet attributes)
throws BadLocationException
{
replace(fb, offset, 0, text, attributes);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attributes)
throws BadLocationException
{
Document doc = fb.getDocument();
// add check here check the length of the text currently in the document
// with the length of your text String to make sure the total is not above the maximum
// you should modify the class to pass a paramenter when you create the class for the
// maximum length so the class can be reused
if (isDigit( text ))
super.replace(fb, offset, length, text, attributes);
else
Toolkit.getDefaultToolkit().beep();
}
private boolean isDigit(String text)
{
for (int i = 0; i < text.length(); i++)
{
if (! Character.isDigit( text.charAt(i) ) )
return false;
}
return true;
}
private static void createAndShowGUI()
{
JTextField textField = new JTextField(15);
AbstractDocument doc = (AbstractDocument) textField.getDocument();
doc.setDocumentFilter( new DigitFilter() );
JFrame frame = new JFrame("Integer Filter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout( new java.awt.GridBagLayout() );
frame.add( textField );
frame.setSize(220, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args) throws Exception
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类DigitFilter扩展了DocumentFilter
{
@凌驾
public void insertString(FilterBypass fb、int offset、字符串文本、AttributeSet属性)
抛出BadLocationException
{
替换(fb、偏移、0、文本、属性);
}
@凌驾
公共void替换(FilterBypass fb、整数偏移量、整数长度、字符串文本、属性集属性)
抛出BadLocationException
{
Document doc=fb.getDocument();
//在此处添加检查检查文档中当前文本的长度
//使用文本字符串的长度,以确保总数不超过最大值
//在为类创建类时,应该修改该类以传递参数
//最大长度,以便可以重用该类
如果(isDigit(文本))
super.replace(fb、偏移量、长度、文本、属性);
其他的
getDefaultToolkit().beep();
}
专用布尔值isDigit(字符串文本)
{
对于(int i=0;icreateAndShowGUI());
/*
invokeLater(新的Runnable()
{
公开募捐
{
createAndShowGUI();
}
});
*/
}
}
在进行处理之前,您需要添加单独的逻辑(DocumentFilter外部),以确保帐户长度至少为8位
有关限制字符数的筛选器示例,请阅读上Swing教程的部分。这里的逻辑需要与这里的示例相结合。您需要创建一个文档过滤器并将其添加到textfields文档中(这样做时不要忘记转换为AbstractDocument)。谢谢camickr和Abra。我正在研究文档过滤器以获得结果。我将在成功时更新它。我成功地完成了上述功能。谢谢你@camickr.Hi camickr,正如你所说,我实现了DocumentFilter,使2个文本字段只接受数字和有限字符。现在出现了一个新问题,在将所有数据提交到数据库后,我试图通过setText(“”)清除所有textfield。这里它只在这两个字段中不起作用。我猜是因为限制字符代码的实现。然后你错误地实现了逻辑。您需要允许在文档中插入空字符串。再次感谢。code=if(isdigit())后跟字符限制n only no的代码……当我清除txt时,它将进入else部分,所以我调用super.replace()方法解决了这个问题。伟大的兄弟