Java KeyListener 🚀

preview_player
Показать описание
Java KeyListener keylistener key listener interface GUI swing tutorial for beginners

#Java #KeyListener #keylistener #key #listener #interface #GUI #swing #tutorial #beginners

public class Main{

public static void main(String[] args) {

new MyFrame();
}
}

public class MyFrame extends JFrame implements KeyListener{


JLabel label;
ImageIcon icon;

MyFrame(){


label = new JLabel();
}

@Override
public void keyTyped(KeyEvent e) {
//keyTyped = Invoked when a key is typed. Uses KeyChar, char output
break;
break;
break;
break;
}

}

@Override
public void keyPressed(KeyEvent e) {
//keyPressed = Invoked when a physical key is pressed down. Uses KeyCode, int output
break;
break;
break;
break;
}
}

@Override
public void keyReleased(KeyEvent e) {
//keyReleased = called whenever a button is released
}
}
Рекомендации по теме
Комментарии
Автор

public class Main{

public static void main(String[] args) {

new MyFrame();
}
}
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame implements KeyListener{


JLabel label;
ImageIcon icon;

MyFrame(){

this.setSize(500, 500);
this.setLayout(null);
this.addKeyListener(this);

icon = new ImageIcon("rocket.png");

label = new JLabel();
label.setBounds(0, 0, 100, 100);
label.setIcon(icon);

//label.setOpaque(true);

this.add(label);
this.setVisible(true);
}

@Override
public void keyTyped(KeyEvent e) {
//keyTyped = Invoked when a key is typed. Uses KeyChar, char output
switch(e.getKeyChar()) {
case 'a': label.setLocation(label.getX()-10, label.getY());
break;
case 'w': label.setLocation(label.getX(), label.getY()-10);
break;
case 's': label.setLocation(label.getX(), label.getY()+10);
break;
case 'd': label.setLocation(label.getX()+10, label.getY());
break;
}

}

@Override
public void keyPressed(KeyEvent e) {
//keyPressed = Invoked when a physical key is pressed down. Uses KeyCode, int output
switch(e.getKeyCode()) {
case 37: label.setLocation(label.getX()-10, label.getY());
break;
case 38: label.setLocation(label.getX(), label.getY()-10);
break;
case 39: label.setLocation(label.getX()+10, label.getY());
break;
case 40: label.setLocation(label.getX(), label.getY()+10);
break;
}
}

@Override
public void keyReleased(KeyEvent e) {
//keyReleased = called whenever a button is released
System.out.println("You released key char: " + e.getKeyChar());
System.out.println("You released key code: " + e.getKeyCode());
}
}

BroCodez
Автор

Dude!!!! That was the most straightforward and clean java tutorial I have seen yet. I'm gonna browse through your stuff now.

ebrahimmahasin
Автор

love the way you teach us! thanks, God bless you!

danielm.
Автор

this is the first KeyListener tutorial that has actually worked for me!!!

bgpplayz
Автор

The clearest and easiest way to learn Java on Youtube. Thanks so much Bro

jassemtoumi
Автор

Not gonna lie, u taught this better than my prof and helped me pass my exam!!

battiest
Автор

This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

pavelkvasnicka
Автор

Thanks for the tutorial, I could not understand any of the documents but your videos are really clear and understandable

mhn
Автор

exelente aula, eu nao entendo ingles mas entendi as referencias, muito obrigado.

Liv.Junior
Автор

if you ever feel useless just remember that there are people who disliked this masterpiece

lightningxr
Автор

Thank u a lot bro! Keep it up! U r helping us newbies a lot. Your explanation is clear, good and simple.

gloriousdude
Автор

Bro, You actually help me a lot in my Java journey

meshkaygaming
Автор

thanks for teaching me this :D helps out a lot. appreciate the work you do!

Chuboi
Автор

Thank you for the content, really appreciate it.

henriquefelinimena
Автор

I had a question, why is the label moving upwards when you place -1 on y-axis(label.getY()-1) in key I thought it should move downwards since the Vertical axis in Y shows negatives at the bottom or is usually on the bottom.... -1, -2, -3, -4, -5 (The arrow points downwards in Mathematical point of

VDJWES_officialchannel
Автор

2 hours of google grinding ... I could have just watched your video. Great work bro!

SchmiddiMusic-qucp
Автор

Thanks bro! You really help me so much! U are a amazing person and teacher!

gilantonyborba
Автор

Thank you sir...for teaching us keylistener

ghoshenterprise
Автор

All weorked very well, except I had to adjust the size of the the rocket. I used an Image of the x-wing and sized it down within photoshop

amedeekreuzer
Автор

Hey man! You make really good tutorials!! Recently I've been trying this out and it works, but one thing I can't seem to wrap my head around is the delay of the character. For a half a second it seems like the character has a delay for moving when you hold the key, and I am curious how I would fix this, any help would be appreciated

javacrow_