请教各位一个问题 tfield[0].getText().trim().length()==0 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 tfield[0].getText().trim()==""只能为数字的话,可判断其每一个字符的ascii码 import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.applet.*;public class test123 extends Applet { JTextField tf=new JTextField(20); JButton b=new JButton("test"); public void init(){ add(tf); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String tempstr=tf.getText().trim(); if(tempstr.length()==0){System.out.println("为空,不合法");return;} try{ double ddd=Double.parseDouble(tempstr); }catch(NumberFormatException nfe) { System.out.println("只能是数字"); } } }); add(b); }}/*<APPLET CODE="test123" WIDTH="300" HEIGHT="300"></APPLET>*/ 给你一段代码看看,我是怎么控制的import javax.swing.text.*;import java.awt.Toolkit;/** * <p>Title: </p> * <p>Description: 控制输入框的输入形式和长度</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author L.M.X * @version 1.0 */public class LimitedDocument extends PlainDocument { private int _maxLength = 5; public LimitedDocument(){ super(); } public LimitedDocument( int maxLength ){ super(); this._maxLength = maxLength; } public void insertString(int offset,String s,AttributeSet attributeSet) throws BadLocationException{ try{ if(super.getLength()+s.length()>_maxLength) return; Integer.parseInt(s); } catch(Exception e){ Toolkit.getDefaultToolkit().beep(); return; } super.insertString(offset,s,attributeSet); } } 然后就可以用了 textField.setDocument(new LimitedDocument()); java 源码问题 SinglePixelPackedSampleModel类 《JAVA编程思想》中第四章第二题的答案错了。 (帮帮新手)谁给我通俗的讲一下String与StringBuffer的区别。详细点 对于进度条的无奈,请进来看看 关于java缩放 这个过程那里错了啊..请高手来帮帮.!! 如何在jtabbedpane里面重复使用同一个componet? 如何获取局域网内全部服务器当前时间(Unix主机)? 要用java语言实现永无溢出的智能加法算数程序 Java屌丝求帮忙:请高手帮忙修改一个“截图功能”的优化调用效果. 没有分的,大家不要来! JTextArea如何定位到某一行
import javax.swing.*;
import javax.swing.event.*;
import java.applet.*;public class test123 extends Applet
{ JTextField tf=new JTextField(20);
JButton b=new JButton("test");
public void init(){
add(tf);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String tempstr=tf.getText().trim();
if(tempstr.length()==0){System.out.println("为空,不合法");return;}
try{
double ddd=Double.parseDouble(tempstr);
}catch(NumberFormatException nfe)
{
System.out.println("只能是数字");
}
}
});
add(b);
}
}
/*
<APPLET CODE="test123" WIDTH="300" HEIGHT="300">
</APPLET>
*/
import javax.swing.text.*;
import java.awt.Toolkit;/**
* <p>Title: </p>
* <p>Description: 控制输入框的输入形式和长度</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author L.M.X
* @version 1.0
*/public class LimitedDocument extends PlainDocument { private int _maxLength = 5; public LimitedDocument(){
super();
} public LimitedDocument( int maxLength ){
super();
this._maxLength = maxLength;
} public void insertString(int offset,String s,AttributeSet attributeSet) throws BadLocationException{
try{
if(super.getLength()+s.length()>_maxLength) return;
Integer.parseInt(s);
}
catch(Exception e){
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,attributeSet);
}
}