Created
April 18, 2014 02:51
-
-
Save jeremiahmarks/11022597 to your computer and use it in GitHub Desktop.
Java snippet to get help with KeyListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.JLabel; | |
import java.awt.*; | |
import java.awt.event.*; | |
public class KeyLis{ | |
public static void main(String[] args){ | |
JFrame frame=new JFrame("Test Frame"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.getContentPane().add(new KeyTestPanel()); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
} | |
class KeyTestPanel extends JPanel{ | |
JLabel testLabel; | |
private final int WIDTH=300, HEIGHT=300; | |
private int x=0; | |
public KeyTestPanel(){ | |
MyKeyListener listener = new MyKeyListener(); | |
addKeyListener(listener); | |
setPreferredSize(new Dimension(WIDTH, HEIGHT)); | |
testLabel = new JLabel("No Key pressed yet"); | |
add(testLabel); | |
} | |
private void TestUpdate(){ | |
testLabel.setText("Updated " + Integer.toString(x)); | |
x+=1; | |
} | |
private class MyKeyListener implements KeyListener{ | |
public void keyPressed(KeyEvent e){ | |
TestUpdate(); | |
} | |
public void keyReleased(KeyEvent e){ | |
/*flap();*/ | |
} | |
public void keyTyped(KeyEvent e){ | |
/*flap();*/ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment