请问如何判断jTextField中输入的是数字呢? try{ Double.parse...}catch (Exception e){ //非数字字符。}如果要JTextArea只可键入数字,加多一个keylistener每次键入作判断。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以捕捉KeyEvent ke 事件,当输入的不是数字的时候,调用ke.consume()import java.awt.*;import java.awt.event.*;class KeyFilter { public static void main(String[] args) { final TextField tf = new TextField(20); tf.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent ke) { if ((ke.getKeyChar()<0x30)||(ke.getKeyChar()>0x39)) ke.consume(); } }); Frame f = new Frame(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); f.add(tf); f.pack(); f.setVisible(true); f.show(); }} 这种方法也可以实现,但是我觉得darkway(黑道) 的方法更加简单,方便。public class JTextArea_DocumentIntOnly extends JFrame { JTextField tf = new JTextField(30); public JTextArea_DocumentIntOnly() { Container contentPane = getContentPane(); JLabel label = new JLabel("Enter an Integer:"); //tf.setDocument(new IntegerDocument()); tf.setDocument(new DateDocument(this.tf)); contentPane.setLayout(new FlowLayout()); contentPane.add(label); contentPane.add(tf); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); } public static void main(String[] args) { JTextArea_DocumentIntOnly JTextArea_DocumentIntOnly1 = new JTextArea_DocumentIntOnly(); JTextArea_DocumentIntOnly1.pack() ; JTextArea_DocumentIntOnly1.show() ; }}class IntegerDocument extends PlainDocument { public void insertString(int offset, String s, AttributeSet attributeSet)throws BadLocationException { try { Integer.parseInt(s); super.insertString(offset, s, attributeSet); } catch(Exception ex) { // only allow integer values Toolkit.getDefaultToolkit().beep(); System.out.println("Integer Only!"); return; } }} TCP/HTTP 代理服务器 新手求助T T 关于unicode的问题 hi guys,请教一个String.replace(),现在我想对一个大String进行一些字符替换 泛型小问题~~初级~ 提问 挺急的 如何提取数据库的主外键? MVC在GUI中的应用(请教) applet中jbutton图标显示问题 在xp下怎么找不到autoexec.bat文件呢? 如何配置才能在tomcat下使用javabena,help,在线等待! 请教线程重起的程序该这么写 用POI设置EXCEL文件中单元格的颜色
import java.awt.*;
import java.awt.event.*;class KeyFilter {
public static void main(String[] args) {
final TextField tf = new TextField(20);
tf.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent ke) {
if ((ke.getKeyChar()<0x30)||(ke.getKeyChar()>0x39))
ke.consume();
}
});
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.add(tf);
f.pack();
f.setVisible(true);
f.show();
}
}
JTextField tf = new JTextField(30); public JTextArea_DocumentIntOnly() {
Container contentPane = getContentPane();
JLabel label = new JLabel("Enter an Integer:");
//tf.setDocument(new IntegerDocument());
tf.setDocument(new DateDocument(this.tf));
contentPane.setLayout(new FlowLayout());
contentPane.add(label);
contentPane.add(tf);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}
public static void main(String[] args) {
JTextArea_DocumentIntOnly JTextArea_DocumentIntOnly1 = new JTextArea_DocumentIntOnly();
JTextArea_DocumentIntOnly1.pack() ;
JTextArea_DocumentIntOnly1.show() ;
}
}class IntegerDocument extends PlainDocument {
public void insertString(int offset, String s, AttributeSet attributeSet)throws BadLocationException {
try {
Integer.parseInt(s);
super.insertString(offset, s, attributeSet);
}
catch(Exception ex) { // only allow integer values
Toolkit.getDefaultToolkit().beep();
System.out.println("Integer Only!");
return;
}
}
}