怎么才能让文本框只能输入汉字,而不能输入数字或者字母,java Swing
1个回答
展开全部
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class JFormattedText extends JFrame {
private JFormattedTextField text;
public JFormattedText() {
JPanel panel = new JPanel();
text = new JFormattedTextField(new java.text.DecimalFormat("#0"));
// text = new JFormattedTextField();
// text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
text.setFont(new java.awt.Font("宋体", 0, 12)); // NOI18N
text.setMaximumSize(new java.awt.Dimension(50, 21));
text.setMinimumSize(new java.awt.Dimension(50, 21));
text.setPreferredSize(new java.awt.Dimension(50, 21));
//过滤输入的字符
text.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Administrator
*/
public class JFormattedText extends JFrame {
private JFormattedTextField text;
public JFormattedText() {
JPanel panel = new JPanel();
text = new JFormattedTextField(new java.text.DecimalFormat("#0"));
// text = new JFormattedTextField();
// text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
text.setFont(new java.awt.Font("宋体", 0, 12)); // NOI18N
text.setMaximumSize(new java.awt.Dimension(50, 21));
text.setMinimumSize(new java.awt.Dimension(50, 21));
text.setPreferredSize(new java.awt.Dimension(50, 21));
//过滤输入的字符
text.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询