判断输入的是不是字母(图形相关)

mastersai2004 2004-08-03 01:56:36
当txt输入为不是字母的时候,打印请输入字母
但是有些问题,大家帮忙看看

PS:还是前面的例子,只是改了一句,顺便谢谢长空兄



import java.awt.*;
import java.awt.event.*;
public class Test
{
public static void main(String args [])
{
FrameDemo f = new FrameDemo();
f.show();
}
}
class FrameDemo extends Frame
{
Button b = new Button("ok");
TextField txt = new TextField(20);
public FrameDemo()
{
setSize(200,100);
setLayout(new FlowLayout());
add(txt);
add(b);

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!(txt.getText().length() >='A' && txt.getText().length() <= 'Z')||

(txt.getText().length() >='a' && txt.getText().length() <= 'z'))
// 就是这句有问题



{
System.out.println("请输入字母");

}
}
});

}
}

...全文
154 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sealwzq 2004-08-03
  • 打赏
  • 举报
回复
package myjava;

import java.awt.*;
import java.awt.event.*;

public class TestWeb
{
public static void main(String args[])
{
FrameDemo f = new FrameDemo();
f.show();
}
}

class FrameDemo extends Frame implements ActionListener
{
Button b = new Button("ok");
TextField txt = new TextField(20);
public FrameDemo()
{
setSize(200, 100);
setLayout(new FlowLayout());
add(txt);
add(b);
txt.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
String textValue = txt.getText();
int len = textValue.length();
if (c == '\b'){}
else
{
if (Character.isLetter(c))
{
return;
}
else
{
e.consume();
System.out.println("Please input!");
}
}

}
});
txt.addActionListener(this);
}

public void actionPerformed(ActionEvent evt)
{
System.out.println(txt.getText());
}
}
sealwzq 2004-08-03
  • 打赏
  • 举报
回复
你可以判断,如果是删除键就作相应的
可以增加一句
if(c=='\b'){你的代码}
mastersai2004 2004-08-03
  • 打赏
  • 举报
回复
to: sealwzq(幻影)
现在编译成功了
但是有个问题,不能按退格符或者delete阿,如果输错的话……
mastersai2004 2004-08-03
  • 打赏
  • 举报
回复
to: sealwzq(幻影)编译错误
--------------------Configuration: j2sdk1.4.2 <Default>--------------------
E:\Test.java:39: cannot resolve symbol
symbol: class OnlyInputCharacterField
String textValue = OnlyInputCharacterField.this.getText();
^
1 error

Process completed.

sealwzq 2004-08-03
  • 打赏
  • 举报
回复
我把完整的改过的代码给你:个人意见

import java.awt.*;
import java.awt.event.*;

public class TestWeb {
public static void main(String args[]) {
FrameDemo f = new FrameDemo();
f.show();
}
}

class FrameDemo
extends Frame {
Button b = new Button("ok");
TextField txt = new TextField(20);
public FrameDemo() {
setSize(200, 100);
setLayout(new FlowLayout());
add(txt);
add(b);
txt.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isLetter(c)) {
return;
}
else {
e.consume();
System.out.println("Please input!");
}
}
});

/*
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!(txt.getText().length() >='A' && txt.getText().length() <= 'Z')||
(txt.getText().length() >='a' && txt.getText().length() <= 'z'))
{
System.out.println("请输入字母");
}
}
});*/

}
}
sealwzq 2004-08-03
  • 打赏
  • 举报
回复
if (Character.isLetter(c))
{ return;}
else
{ e.consume();}

在这里的else中漏了一句
System.out.println("请输入字母!");
sealwzq 2004-08-03
  • 打赏
  • 举报
回复
你把这句
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(!(txt.getText().length() >='A' && txt.getText().length() <= 'Z')||

(txt.getText().length() >='a' && txt.getText().length() <= 'z'))
// 就是这句有问题
改为:
b.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
String textValue = OnlyInputCharacterField.this.getText();
int len = textValue.length();
if (Character.isLetter(c))
{ return;}
else
{ e.consume();}
}
});
试试

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧