1

I have tried this code to draw a string on my frame using KeyListener interface such that whenever I hit a typeable key on keyboard, it should appear on frame but it doesn't work even though there are no errors.

Can someone tell what's the mistake?

Below is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class KeyevntFrame2 extends Frame {
    Button b = new Button("ok");
    Button b1 = new Button("hey");
    char ch;
    String s = "";

    public KeyevntFrame2() {
        setTitle("understand key events");
        setSize(800, 600);
        addKeyListener(new KeyHandler());
        setFont(new Font("Arial", Font.PLAIN, 35));
        setForeground(Color.BLACK);
        add(b);
        add(b1);

        b.setBounds(200, 200, 100, 100);
        b1.setBounds(200, 700, 100, 100);
        setLayout(null);

        b.addActionListener(new KeyHandler());
        b1.addActionListener(new KeyHandler());
    }

    class KeyHandler implements KeyListener, ActionListener {
        public void keyPressed(KeyEvent e) {
            ch = e.getKeyChar();
            s = s + ch;
            repaint();
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyTyped(KeyEvent e) {

        }

        public void paint(Graphics g) {
            g.drawString(s, 300, 200);
            g.setFont(new Font("Arial", Font.PLAIN, 35));
        }

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(b1, "thank you for using java");
        }
    }

    public static void main(String a[]) {
        KeyevntFrame2 f = new KeyevntFrame2();
        f.setVisible(true);
    }
}
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
rose123
  • 11
  • 2

1 Answers1

1
  1. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. This advice is especially relevant to this GUI, given that the frame does not have enough height to display the second button.
  2. Don't mix AWT (Frame) and Swing (JOptionPane) components in one GUI. Choose a GUI toolkit and stick with it.
  3. Always use @Override notation when changing the behavior of existing methods or implementing the methods of an interface. Doing so would have warned you that neither the KeyListener nor ActionListener interfaces define a public void paint(Graphics) method!
  4. Defining a combined KeyListener and ActionListener does not make much sense, and has confused you into thinking that calling Button.addActionListener(..) with the combined listener will also have the effect of adding it as a KeyListener. It won't.
  5. new Font("Arial", Font.PLAIN, 35) for cross-platform robustness, that should be new Font(Font.SANS_SERIF, Font.PLAIN, 35) (e.g OS X will typically not have the Arial font installed, and users would prefer to see Helvetica in any case.)
  6. It is not necessary to set the font of the frame, and also the font in the paint method. Just do it once in the frame.
  7. Since the frame itself is not focusable, calling addKeyListener(..) will have no effect. Better to use Swing and implement key bindings in any case.
  8. When custom painting, always call the super method first.
  9. Swing and AWT GUIs should be started on the EDT.
  10. "it doesn't work even though there are no errors." There are plenty of errors in the code seen above, it's just that they are neither compilation errors, nor run-time errors that throw exceptions. Plenty can still go wrong with code even if the compiler or virtual machine does not identify them. This is why 'cut & paste' coding sans understanding what the code does, never works. Hit the tutorials and read the Java docs.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433