一个极其挠头的问题,帮up有分。 设置一下状态位不就行了吗?不过java的消息机制应该有更好的方法实现! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有没有办法在JDialog关闭时把回车键吃掉? 用keyPressed可以吗?keyPressed不会有这个问题。 I use copy&paste to add some non-digits, can you detect?deal with keyReleased is not good. my Suggestion:subclass JTextField, add DocumentListener to detect if input is valid 检查的代码是什么意思?是说,检查输入的是不是数字么?如果要是那样的话,大可不必阿你可以在输入是就得知按键是什么,在那时判断我的意思就是,当输入的不是数字时,自动让JDialog弹出,不用Enter键 studying!!!!! upupup! 判断是否为"0"-"9"和Enter键的值不就可以吗? 以前写的import javax.swing.text.*;import java.math.BigDecimal;public class NumberFormatDocument extends PlainDocument{ private int textLength = -1; private int scale = -1; public NumberFormatDocument() { init(); } public NumberFormatDocument(int len) { this.textLength = len; init(); } public NumberFormatDocument(int len, int scale) { if(len > 0) this.textLength = len; if(scale > 0) this.scale = scale; init(); } public void setLength(int len) { if(len > 0) this.textLength = len; init(); } public void setScale(int scale) { if(scale > 0) this.scale = scale; init(); } protected void init() { try { if(0 < scale) { String n = "0."; for(int i = 0; i < scale; i++) { n += "0"; } this.replace(0,this.getLength(),n,null); } else this.replace(0,this.getLength(),"0",null); } catch(Exception e) { } } public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null || "".equals(str)) { if (this.getLength() == 0) { str = "0"; if (scale > 0) { str += "."; for (int i = 0; i < scale; i++) { str += "0"; } } insertNumber(offs, str, a); } return; } insertNumber(offs, str, a); } private void insertNumber(int offs, String str, AttributeSet a) throws BadLocationException { String text = ""; int len = 0; char strFirstChar = str.charAt(0); if (0 == offs) { if (strFirstChar == '-' || strFirstChar == '+') { text = str.substring(1); } else if (strFirstChar == '.' && scale > 0) { str = "0" + str; if (str.length() > 1) { text = str; } } else { text = str; } len = text.length(); } else { text = this.getText(0, offs) + str + this.getText(offs, getLength() - offs); if (this.getText(0, 1).equals("-") || this.getText(0, 1).equals("+")) { len = text.length() - 1; } else { len = text.length(); } } int dot = text.indexOf('.'); if(dot > 0) { if ( -1 != textLength && dot > textLength) return; if(scale < 0 || (scale > 0 && len - dot - 1 > scale)) { return; } } else { if ( -1 != textLength && len > textLength) return; } if (!"".equals(text)) { try { new BigDecimal(text); } catch (Exception e) { return; } } super.insertString(offs, str, a); }} 问题解决了,用shadowcain()提供的方法,将keyrelease方法改成了keypress方法,谢谢大家!散分 jtextarea实现撤销和恢复? 如何解决脱离书本就不会写程序的问题? 怎样判断上网的连接方式 大家看看这个编程题 super.paint(g);在这个程序中是什么意思啊? 如何用Java在applet上调用系统上的应用程序 菜鸟遇到一个设计链表的题,请各位高手帮忙解答下,谢先!! 那位大哥告诉我java 控制台有啥用? 如何将一个int转成byte? 一个java初级问题 ejb初学,请教关于sessionbean的问题??? 一个java解释器的问题
deal with keyReleased is not good.
subclass JTextField,
add DocumentListener to detect if input is valid
是说,检查输入的是不是数字么?
如果要是那样的话,大可不必阿
你可以在输入是就得知按键是什么,在那时判断
我的意思就是,当输入的不是数字时,自动让JDialog弹出,不用Enter键
import javax.swing.text.*;
import java.math.BigDecimal;public class NumberFormatDocument
extends PlainDocument
{
private int textLength = -1; private int scale = -1; public NumberFormatDocument()
{
init();
} public NumberFormatDocument(int len)
{
this.textLength = len;
init();
} public NumberFormatDocument(int len, int scale)
{
if(len > 0)
this.textLength = len;
if(scale > 0)
this.scale = scale;
init();
} public void setLength(int len)
{
if(len > 0)
this.textLength = len;
init();
} public void setScale(int scale)
{
if(scale > 0)
this.scale = scale;
init();
} protected void init()
{
try
{
if(0 < scale)
{
String n = "0.";
for(int i = 0; i < scale; i++)
{
n += "0";
}
this.replace(0,this.getLength(),n,null);
}
else
this.replace(0,this.getLength(),"0",null);
}
catch(Exception e)
{ }
} public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException
{
if (str == null || "".equals(str))
{
if (this.getLength() == 0)
{
str = "0";
if (scale > 0)
{
str += ".";
for (int i = 0; i < scale; i++)
{
str += "0";
}
}
insertNumber(offs, str, a);
}
return;
}
insertNumber(offs, str, a);
} private void insertNumber(int offs, String str, AttributeSet a)
throws BadLocationException
{
String text = "";
int len = 0;
char strFirstChar = str.charAt(0);
if (0 == offs)
{
if (strFirstChar == '-' || strFirstChar == '+')
{
text = str.substring(1);
}
else if (strFirstChar == '.' && scale > 0)
{
str = "0" + str;
if (str.length() > 1)
{
text = str;
}
}
else
{
text = str;
}
len = text.length();
}
else
{
text = this.getText(0, offs) + str +
this.getText(offs, getLength() - offs);
if (this.getText(0, 1).equals("-") ||
this.getText(0, 1).equals("+"))
{
len = text.length() - 1;
}
else
{
len = text.length();
}
}
int dot = text.indexOf('.');
if(dot > 0)
{
if ( -1 != textLength && dot > textLength)
return;
if(scale < 0 || (scale > 0 && len - dot - 1 > scale))
{
return;
}
}
else
{
if ( -1 != textLength && len > textLength)
return;
} if (!"".equals(text))
{
try
{
new BigDecimal(text);
}
catch (Exception e)
{
return;
}
} super.insertString(offs, str, a);
}
}