简体   繁体   English

该程序的KeyAdapter部分出错

[英]Error in KeyAdapter part of this program

Compilation and Execution is successful in this program . 该程序的编译和执行成功。 But When i type some characters the frame isn't showing those character within it. 但是当我键入一些字符时,框架中没有显示这些字符。 why ? 为什么呢? what is the error.? 有什么错误。?

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

class frameadapter extends WindowAdapter
{
    newframe newthis;

    public frameadapter(newframe n)
    {
        newthis=n;
    }
    public void windowClosing(WindowEvent we)
    {
        newthis.setVisible(false);
        System.exit(0);
    }

}


class keyadapter extends KeyAdapter
{
    newframe keythis;
    public keyadapter(newframe n1)
    {
        keythis=n1;
    }

    public void KeyTyped(KeyEvent ke)
    {
        keythis.keymsg+=ke.getKeyChar();
        System.out.println(keythis.keymsg);
        keythis.repaint();
    }   
}




public class newframe extends Frame implements MouseListener
{
    int mouseX;
    int mouseY;
    String keymsg="This is a Test";
    String msg="";
    public newframe()
    {
        addKeyListener(new keyadapter(this));
        addWindowListener(new frameadapter(this));
        addMouseListener(this);
        this.setSize(600,600);
        this.setVisible(true);
    }

    public void paint(Graphics g)
    {
        g.drawString(keymsg,100,100);
        g.drawString(msg, 500, 200);
    }


    public void mouseClicked(MouseEvent e) {
        mouseX=this.getX();
        mouseY=this.getY();
        msg="MOUSE CLICKED AT";
        repaint();
    }


    public void mousePressed(MouseEvent e) {
        mouseX=this.getX();
        mouseY=this.getY();
        msg="MOUSE PRESSED AT";
        repaint();
    }

    public void mouseReleased(MouseEvent e) {
        mouseX=this.getX();
        mouseY=this.getY();
        msg="MOUSE RELEASED AT";
        this.setForeground(Color.WHITE);
        this.setBackground(Color.BLACK);
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
        mouseX=this.getX();
        mouseY=this.getY();
        msg="MOUSE ENTERED AT";
        repaint();
    }


    public void mouseExited(MouseEvent e) {
        mouseX=this.getX();
        mouseY=this.getY();
        msg="MOUSE EXITED AT";
        repaint();
    }

    public static void main(String args[])    
    {
        newframe n=new newframe();
    }
}

The Error i think is in the Keyadapter class.But Unable to find a solution. 我认为错误是在Keyadapter类中。但是找不到解决方案。

  1. KeyListener only responds to key events when the component it is registered to is focusable AND has keyboard focus KeyListener仅在其注册到的组件可聚焦并且具有键盘焦点时才响应键事件
  2. Frame is not focusable, from a key event standpoint, this makes it impossible for it to, but default, receive key event notification... 从关键事件的角度来看, Frame是无法聚焦的,这使其无法(但默认情况下)接收关键事件通知...

Unless you have some desperate need to do so, I would recommend against using Frame and instead, use a JFrame as your window, as AWT is 15+ years out of date and generally is not longer used. 除非您迫切需要这样做,否则我建议您不要使用Frame ,而应使用JFrame作为窗口,因为AWT已过期15年以上,通常不再使用。 Take a look at Creating a GUI With JFC/Swing for more details 看一下使用JFC / Swing创建GUI的更多细节

Instead, start with a JPanel , override it's paintComponent method be perform your custom painting there. 而是从JPanel开始,重写它的paintComponent方法,以在那里执行自定义绘制。 Take a look at Performing Custom Painting for more details. 有关更多详细信息,请参见“ 执行自定义绘画 ”。

Use the key bindings API to regsiter key actions against the panel. 使用按键绑定API对面板重新注册按键操作。 This will allow you to define the focus level required for the panel to receive key event notification 这将允许您定义面板接收按键事件通知所需的焦点级别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM