新手上路,请教如何获取文本框TextField的内容为int型 int i = Integer.parseInt(TextField.getText().trim()) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 hui3zhihui(慧慧)的答案正确,就这么简单 我的意思是输入字符串1234时,获取INT型的值也就是字符串转换为整型谢谢各位 我按照hui3zhihui(慧慧)的答案试试 干脆给楼主一个只能输入0-9的JTextField类吧!可以设置输入个数import javax.swing.*;import javax.swing.text.*;import java.awt.*;public class test extends JTextField{ private numDoc doc = null; public test() { super(12); setDocument( doc = new numDoc()); } public test(int maxLen) { super(12); setDocument( doc = new numDoc(maxLen)); } public void setMaxLen( int maxLen ) { doc.setMaxLen( maxLen ); } public int getMaxLen() { return doc.getMaxLen(); } public static void main( String args[] ) { JFrame f = new JFrame("Only input number characters" ); f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); Container c = f.getContentPane(); test test1; c.add( test1 = new test(),BorderLayout.NORTH ); c.add( new test( 10 ),BorderLayout.CENTER ); test1.setMaxLen( 20 ); f.pack(); f.show(); }}class numDoc extends PlainDocument{ int maxLength=16; public numDoc(int maxLen) { maxLength=maxLen; } public numDoc(){} public void setMaxLen( int maxLength ) { this.maxLength = maxLength; } public int getMaxLen() { return maxLength; } public void insertString(int offset,String s,AttributeSet a)throws BadLocationException { int len = getLength(); String str = getText( 0,len ); if ( (str+s).length() > maxLength ) { Toolkit.getDefaultToolkit().beep(); return; } try { Integer.parseInt( str+s ); } catch ( Exception e ) { Toolkit.getDefaultToolkit().beep(); return; } super.insertString(offset,s,a); }} 就是 hui3zhihui(慧慧)的了,呵呵。 没错,是 hui3zhihui(慧慧)的答案。 经过证实, hui3zhihui(慧慧)的答案是正确的。 很感谢各位的帮助!!! tomcatjava(小鱼儿)的代码我试试,也很感谢你为我提供了特别的答案! 让我又学到了一招!!! 那还有一个你们认为很简单的问题:又如何将整型转换为字符串呢! 就是将其在文本框中显示出来!Intege.toString(int i) 计算器程序中的正负转换问题 requestFocusInWindow()会不会引起画面死掉,紧急!!!!!!! 带参数的main() synchronized 问题 小弟请教一个Javac中编译成功,但是java运行程序时报找不到类定义的错误问题? 有没有获取系统时间的函数 用java.util.zip 对一个文件 进行压缩,和解压的例子? 求助java 连接Sqlserver 的错误 求助JAVA 局域网内借助ODBC/jdbc 如何访问sqlserver? 关注一下好吗(我的是交作业的) 问一个简单问题 关于输入输出流的问题?
也就是字符串转换为整型谢谢各位 我按照hui3zhihui(慧慧)的答案试试
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;public class test extends JTextField
{
private numDoc doc = null; public test() {
super(12);
setDocument( doc = new numDoc());
} public test(int maxLen)
{
super(12);
setDocument( doc = new numDoc(maxLen));
}
public void setMaxLen( int maxLen ) {
doc.setMaxLen( maxLen );
} public int getMaxLen() {
return doc.getMaxLen();
} public static void main( String args[] ) {
JFrame f = new JFrame("Only input number characters" );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = f.getContentPane();
test test1;
c.add( test1 = new test(),BorderLayout.NORTH );
c.add( new test( 10 ),BorderLayout.CENTER );
test1.setMaxLen( 20 );
f.pack();
f.show();
}
}class numDoc extends PlainDocument
{
int maxLength=16; public numDoc(int maxLen)
{
maxLength=maxLen;
}
public numDoc(){} public void setMaxLen( int maxLength ) {
this.maxLength = maxLength;
} public int getMaxLen() {
return maxLength;
} public void insertString(int offset,String s,AttributeSet a)throws BadLocationException
{
int len = getLength();
String str = getText( 0,len );
if ( (str+s).length() > maxLength )
{
Toolkit.getDefaultToolkit().beep();
return;
}
try
{
Integer.parseInt( str+s );
}
catch ( Exception e )
{
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}
很感谢各位的帮助!!! tomcatjava(小鱼儿)的代码我试试,也很感谢你为我提供了特别的答案!
让我又学到了一招!!!
就是将其在文本框中显示出来!
Intege.toString(int i)