0

I want the program to draw the key that is pressed on the screen. When I press a key, nothing happens.

Class where the draw code is:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lab5;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;



public class KeyDemoFrame extends JPanel implements KeyListener{

    private JPanel window;
    private String myString = "";

    public KeyDemoFrame(){
        window = new JPanel();
        window.setSize(400,400);
        window.setVisible(true);
        addKeyListener(this);

    }

    public void keyPressed(KeyEvent event){
        myString = String.format("Pressed %s", KeyEvent.getKeyText( event.getKeyCode()));
        repaint();
    }

    public void keyReleased( KeyEvent event){
        myString = String.format("Released %s", KeyEvent.getKeyText( event.getKeyCode()));
        repaint();
    }
    public void keyTyped (KeyEvent event){
        myString = String.format("Typed %s", KeyEvent.getKeyText( event.getKeyChar()));
        repaint();
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.setColor(Color.red);
        g.drawString(myString,100,100);
    }

}

and my main class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lab5;


import javax.swing.JFrame;
import java.awt.BorderLayout;

public class KeyDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame window = new JFrame("A Window");
        KeyDemoFrame keyDemoFrame = new KeyDemoFrame();
        window.add(keyDemoFrame, BorderLayout.CENTER);
        window.setSize(400,500);
        window.setVisible(true);
    }

}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

A couple of things. In your KeyDemoFrame constructor, you're creating a completely separate JPanel.

public KeyDemoFrame(){
        window = new JPanel();
        window.setSize(400,400);
        window.setVisible(true);
        addKeyListener(this);

    }

You need to act upon the current instance of your custom KeyFrameDemo class instead. You also don't need the setSize() and setVisible() calls

public KeyDemoFrame(){
        addKeyListener(this);

    }

The size and visibility will be handled with your JFrame setup over in your main() method.

The other issue is that your KeyDemoFrame panel does not have the focus when you create it and you can't focus it by clicking on it. Your panel needs to have the focus to process your key events. To fix this, you have to request focus after the panel has been made visible.

public static void main(String[] args) {
        // TODO code application logic here
        JFrame window = new JFrame("A Window");
        KeyDemoFrame keyDemoFrame = new KeyDemoFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(keyDemoFrame, BorderLayout.CENTER);
        window.setSize(400,500);
        window.setVisible(true);
        keyDemoFrame.requestFocusInWindow();
    }

Edit: It's also a good idea to specify EXIT_ON_CLOSE so your program terminates when you close the frame.

Edited by Ezzaral: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.