K
KevinSimonson
I'm trying to build a GUI that can read key events and tell which
character was keyed in. I wrote the following code to test my ability
to do that, but when I execute it, click in the <JPanel>, and type in
characters, nothing happens. It seems like I need to set the focus on
the GUI, but I don't remember precisely how to do that. Is there
anybody out there who can see what I'm doing wrong, and how I can get
the characters I enter displayed on my GUI? Thanks in advance for any
pointers you can give me.
Kevin S
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KlBug extends JPanel implements KeyListener
{
private Font courier12;
private char[] enteredString;
private int enteredSize;
private KlBug ()
{
courier12 = new Font( "Courier", Font.PLAIN, 12);
enteredString = new char[ 1000];
enteredSize = 0;
setPreferredSize( new Dimension( 800, 600));
addKeyListener( this);
setBackground( Color.black);
}
public void paintComponent ( Graphics page)
{
super.paintComponent( page);
page.setFont( courier12);
page.setColor( Color.green);
int xLctn = 60;
int yLctn = 60;
for (int ec = 0; ec < enteredSize; ec++)
{ if (enteredString[ ec] != '\n')
{ page.drawString( "" + enteredString[ ec], xLctn, yLctn + 10);
xLctn += 7;
}
else
{ yLctn += 12;
xLctn = 60;
}
}
}
public void keyPressed ( KeyEvent evnt) {}
public void keyReleased ( KeyEvent evnt) {}
public void keyTyped ( KeyEvent evnt)
{
System.out.println( "Key '" + evnt.getKeyChar() + "' typed.");
enteredString[ enteredSize++] = evnt.getKeyChar();
repaint();
}
public static void main ( String[] arguments)
{
KlBug kb = new KlBug();
JFrame kbFrame = new JFrame( "KlBug");
kbFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
kbFrame.getContentPane().add( kb);
kbFrame.pack();
kbFrame.setVisible( true);
}
}
character was keyed in. I wrote the following code to test my ability
to do that, but when I execute it, click in the <JPanel>, and type in
characters, nothing happens. It seems like I need to set the focus on
the GUI, but I don't remember precisely how to do that. Is there
anybody out there who can see what I'm doing wrong, and how I can get
the characters I enter displayed on my GUI? Thanks in advance for any
pointers you can give me.
Kevin S
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class KlBug extends JPanel implements KeyListener
{
private Font courier12;
private char[] enteredString;
private int enteredSize;
private KlBug ()
{
courier12 = new Font( "Courier", Font.PLAIN, 12);
enteredString = new char[ 1000];
enteredSize = 0;
setPreferredSize( new Dimension( 800, 600));
addKeyListener( this);
setBackground( Color.black);
}
public void paintComponent ( Graphics page)
{
super.paintComponent( page);
page.setFont( courier12);
page.setColor( Color.green);
int xLctn = 60;
int yLctn = 60;
for (int ec = 0; ec < enteredSize; ec++)
{ if (enteredString[ ec] != '\n')
{ page.drawString( "" + enteredString[ ec], xLctn, yLctn + 10);
xLctn += 7;
}
else
{ yLctn += 12;
xLctn = 60;
}
}
}
public void keyPressed ( KeyEvent evnt) {}
public void keyReleased ( KeyEvent evnt) {}
public void keyTyped ( KeyEvent evnt)
{
System.out.println( "Key '" + evnt.getKeyChar() + "' typed.");
enteredString[ enteredSize++] = evnt.getKeyChar();
repaint();
}
public static void main ( String[] arguments)
{
KlBug kb = new KlBug();
JFrame kbFrame = new JFrame( "KlBug");
kbFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
kbFrame.getContentPane().add( kb);
kbFrame.pack();
kbFrame.setVisible( true);
}
}