秋千上的onkeyReleased事件 [英] onkeyReleased event in swing

查看:145
本文介绍了秋千上的onkeyReleased事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有一个关于不接受任何字符的文本框的问题.
在这种情况下,我有一个不接受任何字符的文本框,我有一个onkeyReleased事件,每当我按下键盘上的任何键时,它都会显示哎呀,此文本框不接受任何字符!".我该怎么办?

请帮助我.

hi everyone.

i have a problem pertaining to text box that do not accept any characters.
Here is the situation, i have a text box that do not accept any characters, i have an onkeyReleased event that whenever i press any key in keyboard, it says, "Ooops..,this text box do not accept any characters!". How can i do that?

Please help me.

推荐答案

您的类应该实现keyListener适配器,
Your class should implement keyListener adapter to do so,
public class KeyListenerTester extends Frame implements KeyListener


然后实施,


Then implement,

public void keyReleased(KeyEvent Event) {
   int key=evt.getKeyCode();  ///using validation here
                              /// ascii code A=65 ,Z=90 ,a=97 ,z=122 
   if((key>=65&&key<=90)||(key>=97&&key<=122))
   { 
       ///making Textbox empty
       JOptionPane.showMessageDialog(frame,"Ooops..,this text box do not accept any characters!.");
   }


试试吧,
它只是建议,然后回复


Try it,
its just suggestion then reply


显示这很容易,但是我觉得您需要相应地执行代码的行为,以防止输入字符.
这有点琐碎.您将需要改用onKeyPress.

参见:
Showing this is easy, but I feel you need the code to behave accordingly, to prevent characters from entering.
This is a bit less than trivial. You will need to use onKeyPress instead.

See:
<html>
   <head>
      <script type="text/javascript"><!--
         function filterOut(eventInstance) {
            var character = String.fromCharCode(eventInstance.which);
            if (eventInstance.which == 8)
               character = "backspace";
            alert("You want to enter " + character +
               ", Guts_ryo? Relax, this text box do not accept any characters!"); 
            eventInstance.preventDefault();
            eventInstance.returnValue = false;
            return false;
         } //filterOut
      --></script>
   </head>
<body">

<input type="text" onkeypress="filterOut(event)"/>

</body>
</html>



注意退格的特殊处理.您可以使用此代码过滤掉一些字符集,例如仅获得数字或类似的数字.不要忘记在所有情况下都允许退格.

对不起,我看错了;虽然是"Javascript"!糟糕! :-)



-SA



Pay attention for special processing of backspace. You can use this code to filter out some character sets to get, say, digits only or something like that. Don''t forget to allow backspace in all cases.

Sorry, I misread it; though it was "Javascript"! Oops! :-)



—SA


-在要输入文本的组件中添加KeyListener.
-KeyListener必须实现KeyListener及其实现方法:public void keyReleased(KeyEvent oEvent) {/* doany(); */}
-KeyEvent提供事件的来源以及所输入的文本.
-使用获胜数据更新您的文本框.


- add a KeyListener to the Component you''re entering the text in.
- The KeyListener has to implement KeyListener and therefor the method: public void keyReleased(KeyEvent oEvent) {/* doany(); */}
- the KeyEvent provides the source of the event and therefor the entered text.
- update your textbox with the won data.


public void keyReleased(KeyEvent oEvent) {
    Control oControl = (Control)oEvent.getSource();
        final Text oText = (Text) oEvent.widget;
        if ((oText.getText() == null) || oText.getText().equals("")) {
            // setDefaultText (like "")
        } else {
            // use String from oText.getText()
        }
}



问候
Torsten



regards
Torsten


这篇关于秋千上的onkeyReleased事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆