解决方案 »
- 用XML文档保存用户注册信息,验证是否已经注册的问题
- 如何用Java写一个文档抽取工具?(大送分)
- java中如何调用activeX控件?
- 怎样使一个类不能通过new来产生实例,但可以通过本身的getInstance方法得到实例?
- 今天面试题:从客户端输入一字符串“localhost”作为服务器ip连接至服务器,
- com.microsoft.sqlserver.jdbc.SQLServerException: 索引 2 超出范围。
- 类 StringTokenizer 具体是做什么用的?
- JB里,窗口和窗口之间怎么互相调用,然后怎么关闭?
- 在哪里可以下载我需要的原代码?
- 为什么import other.*;不包括import other.oth;????????
- windows怎么运行java程序
- 多维数组定义的一个小问题
public class test5 extends JFrame { public test5() {
JTextField field = new JTextField(10);
field.addKeyListener(new KeyAdapter() {
//添加事件监听
public void keyTyped(KeyEvent e)
{
// System.out.println(e.getKeyCode());
if(!(e.getKeyChar()>=KeyEvent.VK_0 && e.getKeyChar()<=KeyEvent.VK_9))
{
e.consume();//?????怎么没用效啊
}
}
});
this.add(field);
this.setLayout(new FlowLayout());
this.setBounds(100, 200, 300, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args)
{
new test5();
}}
awt的;
public class test5 extends Frame {
public test5() {
TextField field = new TextField(10);
field.addKeyListener(new KeyAdapter() {
//添加事件监听
public void keyTyped(KeyEvent e)
{
System.out.println(e.getKeyCode());
if(!(e.getKeyChar()>=KeyEvent.VK_0 && e.getKeyChar()<=KeyEvent.VK_9))
{
e.consume();
}
}
});
this.add(field);
this.setLayout(new FlowLayout());
this.setBounds(100, 200, 300, 100);
this.add(new Label("AWT 不能输入字符"));
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
} public static void main(String[] args)
{
new test5();
}}
{
System.out.println(e.getKeyCode());
if(Character.isDigit(e.getKeyChar()))
{
e.consume();
}
}
keyReleased 释放
keyTyped 按下+释放