Java Tutorial for Beginners - 43 - GUI - Setting up KeyListener, Keyboard Events, JLabel and Textbox

preview_player
Показать описание
In this tutorial we will go over how to make our program respond to user input
Рекомендации по теме
Комментарии
Автор

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

public class Gui extends JFrame implements KeyListener {
    
JTextField keyText = new JTextField(80);   
JLabel keyLabel = new JLabel("Press fire button");
    
Gui () { 
   
keyText.addKeyListener(this);
setSize(400, 400);  
setVisible(true);
setLocationRelativeTo(null);


BorderLayout layout = new BorderLayout();
setLayout(layout); 
add(keyLabel, BorderLayout.NORTH);
add(keyText, BorderLayout.CENTER);
}

public void keyTyped (KeyEvent e) {
      // not putting anything in this method
}
public void keyPressed (KeyEvent e) {
  

   int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_F){
      
        keyLabel.setText("You pressed the fire button");
    } 
    else {
        keyLabel.setText("You pressed the wrong key");
    }   
          
}
public void keyReleased (KeyEvent txt) {
    // not putting anything in this method
}
public static void main(String[] args) {
    
Gui go = new Gui();

}
}

EJMedia
Автор

Thank you sir, a very clear explanation and also a splendid video !

saschaAi
Автор

Sir, thank you soo much! I've been helped by this tutorial. The only thing I've change was where I added the keylistener. I needed that all the jFrame was onfocus. So I put this lines on construct:

this.addKeyListener(this);
setFocusable(true);

With theses lines, the jTextField it's not necessary.
That was my problem, maybe it's the problem of someone else.

RenatoAlmeida
Автор

10/10 "Best game ever" - IGN (srsly tho, good vid ;)

tornikeonoprishvili
Автор

EJ Media person man, i love your face. Thank you!

APHAS
Автор

If i wanna check the CONTROL KEYS then what will VK follow?

Senshiii
Автор

Almost what I'm looking for, except what if I wanted text frames to update depending on one another. Example: User enters "50" into TextField1(MPH) and TextField2 (KPH) automatically updates to show "80.46" or vise versa User could enter KPH and program calculates MPH? Thanks!

rudytrevino
Автор

Thank you very much for your informative and easy to understands tutorials .
I have three questions,
1. Is there any particular reason for using constructor instead of void method ?
2. If i recall correctly in tutorial 24 you mentioned commands within a constructor can be run without requiring a main method if there is a no user input, would have been possible to run this code without main method if there was no user input ?
3. Why our class needs to inherit from JFrame despite of having java.swing package imported?

regman
Автор

In the last part of the code. you defined Gui go = new Gui ( ); but you never defined "go" earlier. I wrote the same line as yours but it is not working

sakaabdulrahman
Автор

Sir,
To which Class.Object are the following methods attached to?
setSize(400, 400);
setVisible(true);

Don't these need to be referenced as SomeObjectName.setSize(400, 400)? How come you refer to them using just the method name with arguments?
Also, unclear as to what setLayout(layout); means. Its invoking the setLayout method from which class on the layout object?

sraj
Автор

I want use keypress java on different program but not work what should i do?

eobardthawne
Автор

For this code when I ran it, it loaded an empty frame at runtime... but when I moved "setVisible(true);" to the end of the GUI constructor it works

tingtingy
Автор

i keep getting an error message at the line---- message reads "incompatible types. Gui cannot be converted to KeyListener. Leaking this in constructor" anyone else having this problem? am I just making a silly mistake?

SorenBagley
Автор

sir i have 2 question 
the code what do it mean 
second 
when i call the constructor in the main method how it read all the class
thanks

karimaali
Автор

I typed up this lesson's code and posted it on github here:

mollytaylor
Автор

how come keycode is an int and not a char?

jorgeramos
Автор

When are you going to make the mini game tutorials? :D

voizer
Автор

How many more videos do you plan on making for java? I need to learn java in 2 months so i need to know how many videos I need to watch to finish. EJ Media 

manicho
Автор

it doesnt fire at all, with no errors

TheSkyGamez
Автор

instead of keyReleased i typed keyRelesed, and it took me about one hour to find the error. you should really put the source code in the description. often we will save a tons of time and nerves.

stef
join shbcf.ru