我想限制用户应该允许在文本字段中键入的字符。我怎样才能限制JFormattedTextField只接受0或1的8位数字?JFormattedTextFiled限制为0或1
2
A
回答
5
使用DocumentFilter与模式的JTextField
,那么也许有不使用JFormattedTextField
示例程序来帮助您的事业需要:
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class TextFieldLimit
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("JTextField Limit");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
JPanel contentPane = new JPanel();
JTextField tfield = new JTextField(10);
/*
* Here we are using Document Filter
* to specify the size of the content
* i.e. 8 in your case.
*/
((AbstractDocument)tfield.getDocument()).setDocumentFilter(
new DocumentLimitAndInput(8));
contentPane.add(tfield);
frame.getContentPane().add(contentPane);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TextFieldLimit().createAndDisplayGUI();
}
});
}
}
class DocumentLimitAndInput extends DocumentFilter
{
private int maxCharacters;
public DocumentLimitAndInput(int size)
{
maxCharacters = size;
}
public void insertString(FilterBypass fb, int offset
, String text, AttributeSet aset)
throws BadLocationException
{
int size = fb.getDocument().getLength() + text.length();
/*
* If the size is less than or equal to the permissible value
* and the input character is either 0 or 1, only in that
* case we will allow it to go through, else a BEEP
* sound is what a user will hear :-)
*/
if (size <= maxCharacters && (text.equals("0") || text.equals("1")))
super.insertString(fb, offset, text, aset);
else
Toolkit.getDefaultToolkit().beep();
}
public void replace(FilterBypass fb, int offset, int length
, String text, AttributeSet aset)
throws BadLocationException
{
int size = fb.getDocument().getLength() + text.length();
/*
* If the size is less than or equal to the permissible value
* and the input character is either 0 or 1, only in that
* case we will allow it to go through, else a BEEP
* sound is what a user will hear :-)
*/
if (size <= maxCharacters && (text.equals("0") || text.equals("1")))
super.replace(fb, offset, length, text, aset);
else
Toolkit.getDefaultToolkit().beep();
}
}
+0
+1,为什么使用'JFormattedTextField'时可以通过'DocumentFilter'轻松实现:-) – 2012-04-03 15:35:13
+0
对不起,我的编辑,只是忍不住自己,在做艰苦的工作:-) – 2012-04-03 16:07:40
+0
它的工作原理。谢谢 – Sachindra 2012-04-04 17:06:49
相关问题
- 1. MySQL的值限制为1个0
- 2. 强制R将0/0打印为1
- 3. Scipy最小化:如何将x限制为0和1?
- 4. 用户界面:“第1页,共0页”或“第1页1”或“第0页,共0页”为零行?
- 5. 限制联接为1
- 6. NHibernate的1到0或1到1
- 7. 转换numpy的阵列为0或1
- 8. 选择MAX或ORDER BY限制1
- 9. 阵列实为二进制(0/1)
- 10. MySQL - 复选框 - 1或0
- 11. MySQL SQL查询0或1
- 12. 将输入框限制为0-100
- 13. 如何在Java中输出序列'1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 ...'?
- 14. 为什么表达式(0 == 0&1 == 1)评估为False?
- 15. 如何限制输入文本框允许只有0和1
- 16. 为什么0 && 1是1,而1 && 0在红宝石中是0?
- 17. jQuery可排序,限制为1项
- 18. 将itextsharp pdf限制为仅1页
- 19. django + memcached:限制为1 MB的问题
- 20. CSS:将元素限制为1行
- 21. 替换二进制形式0-> 1和1-> 0值 - perl
- 22. 获取布尔值为true或false,而不是1或0
- 23. 浮点值或者默认为1或0
- 24. 将3 .... 0转换为0 ...... 1,Unity3d(Time.time)
- 25. 为什么2 + 1&0结果是0?
- 26. 红宝石为什么0 || 1是0
- 27. 为什么这些多头书写为0或1?
- 28. 如果大于0,则为1 1
- 29. 极限C^N(具有|c| <1)为0(伊莎贝尔)
- 30. 为什么0 + 1 == 49?
参见本[Q&A](HTTP:// stackoverflow.com/q/9995383/230513)。 – trashgod 2012-04-03 20:19:29