为什么String对象的显示中没有按下键字符的附加内容? [英] Why doesn't the String object display that gets pressed key characters appended to it?

查看:123
本文介绍了为什么String对象的显示中没有按下键字符的附加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用键事件处理编写了Java小程序代码,以演示Java中的非缓冲输入.我的代码工作正常并且输出正常,但是我无法在该程序中实现另一个目标:在覆盖的 keyPressed()方法中,我编写以下行: showStatus(s) ; ,其中 s 是全局静态StringBuffer对象,从键盘键入的字符将附加到该对象.但是 showStatus()方法在小程序查看器的状态栏上显示文本.因此,本程序仅可在小应用程序查看器中使用,而不能在Web浏览器中使用.我尝试将语句 g.drawString(String.valueOf(s),10,90); ( g = Graphics类对象作为paint()的参数)(在 paint()方法中)以在画布中显示文本.我希望这可以正常工作,因为s是全局和静态的,但未显示任何输出.我在 paint()中使用了 drawString()方法,无论是否覆盖 boolean action()方法,但我仍然没有得到任何事物.我只需要帮助在小程序的画布中显示键入的文本.

I wrote a Java applet code using Key Event handling to demonstrate Non buffered input in Java. My code is working fine and the output is alright but I'm having trouble accomplishing another objective in this program: in the overriden keyPressed() method, I write the line: showStatus(s); , where s is the global static StringBuffer object which the characters typed from the keyboard are appended to. But the showStatus() method displays text on the status bar of the applet viewer. Therefore, the present program is usable only in an applet viewer and not in a web browser. I tried putting the statement g.drawString(String.valueOf(s),10,90); (g=Graphics class object as argument of paint()) in the paint() method to display the text in the canvas. I expected this to work as s is global and static but it's not showing any output. I used the drawString() method in paint() both with and without overriding the boolean action() method but I still didn't get anything. I just want help in displaying the typed text in the canvas of the applet.

下面是我的代码.请查看它以供参考,并为我提供最好的方法.谢谢.

Below is my code. Please view it for reference and to help me out the best way possible. Thank you.

/* An applet that implements the concept of non buffered user input
using Event handling method.
*/

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

public class NonBufferInput extends Applet implements KeyListener {
    public static StringBuffer s;

    @Override
    public void init() {
        addKeyListener(this);
        s=new StringBuffer();
    }//init

    @Override
    public void keyTyped(KeyEvent K) {}

    @Override
    public void keyPressed(KeyEvent K) {
       char c=K.getKeyChar();
       int i=K.getKeyCode();
       if(i==32)  //space bar
           s.append(' ');
       else if(i==10||i==16||i==20||i==32||i==17||i==524||i==18||i==525||i==27
             ||(i>=112&&i<=123)||i==145||i==19||i==155||i==36||i==33||i==127
             ||i==35||i==34||i==144||(i>=37&&i<=40));  //characters I don't want as input
       else if(i==8) {  //backspace
           if(s.length()!=1)
               s.setLength(s.length()-1);
       }
       else
           s.append(c);
       showStatus("Typed : "+s);
    }

    @Override
    public void keyReleased(KeyEvent K) {}

    @Override
    public void paint(Graphics g) {
        g.drawString("Text will be displayed on status bar as you type.",10,50);
        g.setColor(Color.blue);
        g.drawString(String.valueOf(s),10,80); //PROBLEM
    }//paint

    @Override
    public boolean action(Event event, Object obj) {
        repaint();
        return true;
    }//action
}//class

推荐答案

问题是:EDT(事件调度线程)如何知道它应该repaint applet?

The problem is: how does the EDT (Event Dispatch Thread) know that it should repaint the applet?

将方法keyPressed更改为

   @Override
    public void keyPressed(KeyEvent K) {
       /* .. many lines omitted .. */
       showStatus("Typed : "+s);
       repaint(); // <<- this line is missing!
    }

作为解释:EDT是运行小程序的线程.它从事件队列中获取事件,并将其分派给各种事件侦听器.它的职责之一是对组件进行重新粉刷(或者应要求提供,因为某人/某物已更改了组件),或者在组件被遮盖后又可以再次显示.

As an explanation: the EDT is the thread that runs your applet. It fetches events from the event queue and dispatches them to the various event listeners. One of its responsabilities is the repainting of your components (either on request, since someone / something has changed a component) or after your component has been obscured and is now shown again.

这篇关于为什么String对象的显示中没有按下键字符的附加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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