为什么不显示附加到它的按键字符的 String 对象? [英] Why doesn't the String object display that gets pressed key characters appended to it?

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

问题描述

我使用键事件处理编写了一个 Java 小程序代码来演示 Java 中的非缓冲输入.我的代码运行良好,输出也正常,但我无法在这个程序中实现另一个目标:在重写的 keyPressed() 方法中,我写了一行:showStatus(s); ,其中 s 是全局静态 StringBuffer 对象,从键盘输入的字符将附加到该对象上.但是 showStatus() 方法在小程序查看器的状态栏上显示文本.因此,本程序只能在小程序查看器中使用,而不能在网络浏览器中使用.我尝试将语句 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(Event Dispatch Thread)怎么知道它应该repaint小程序?

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

将您的方法 keyPressed 更改为

It works after changing your method keyPressed to

   @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天全站免登陆