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

public class Praktikum1 extends JFrame implements KeyListener
{
	private String tombol;
	public Praktikum1()
	{
		super ("Menerima Event dari Keyboard");
		addKeyListener(this);
	}

	public static void main(String []args)
	{
		Praktikum1 p1 = new Praktikum1();
		p1.setSize(250, 75);
		p1.setVisible(true);

		p1.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public void keyTyped(KeyEvent e) {}
	public void keyReleased(KeyEvent e) {}
	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			tombol = "Kanan";
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			tombol = "Kiri";
		}
		if(e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			tombol = "Bawah";
		}
		if(e.getKeyCode() == KeyEvent.VK_UP)
		{
			tombol = "Atas";
		}
		repaint ();
	}
	public void paint(Graphics g)
	{
		g.clearRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.BLACK);
		g.drawString("Tombol yang Ditekan : " + tombol, 30, 50);
	}
}

No related posts.

Tulisan terkait dibawakan kepada Anda oleh Yet Another Related Posts Plugin.