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

Поделиться
HTML-код
  • Опубликовано: 24 окт 2024

Комментарии • 60

  • @EJMedia1
    @EJMedia1  10 лет назад +12

    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);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    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();
    }
    }

  • @saschaAi
    @saschaAi 7 лет назад +1

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

  • @RenatoAlmeida49
    @RenatoAlmeida49 6 лет назад +3

    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.

  • @leifleonhart4059
    @leifleonhart4059 3 года назад

    A very good video! Thanks 😉

  • @tornikeonoprishvili6361
    @tornikeonoprishvili6361 7 лет назад +4

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

  • @APHAS17
    @APHAS17 5 лет назад

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

  • @sanchitchawlaa
    @sanchitchawlaa 8 лет назад +1

    thanks alot you saved my night!

  • @JustinTheVlogger
    @JustinTheVlogger 4 года назад

    Thank you for your help.

  • @TheGuroguro12
    @TheGuroguro12 8 лет назад +1

    Thank you very much!

  • @eshaangel4860
    @eshaangel4860 6 лет назад

    This was really helpful. Thanks :)

  • @08regman
    @08regman 9 лет назад

    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?

    • @EJMedia1
      @EJMedia1  9 лет назад

      +08regman
      1) Constructors are just used when objects are created.You should use constructors to organize any setup that you'll need for the rest of the class.
      Any other methods do work for the class after the constructor has done it's business.
      2) Sure you can run classes without a main method - only one of your classes needs a main (the entry point of your program).
      3) Importing Swing classes gives you access to the library but inheritance is differenent

  • @martincerny9866
    @martincerny9866 6 лет назад

    Helped a lot man. Thanks :)

  • @DavidM_
    @DavidM_ 6 лет назад

    What a great video :0

  • @rudytrevino5872
    @rudytrevino5872 9 лет назад

    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!

    • @EJMedia1
      @EJMedia1  9 лет назад

      +Rudy Trevino Hi Rudy can you paste all the code - I haven't seen it in a while -thx

    • @rudytrevino5872
      @rudytrevino5872 9 лет назад

      +EJ Media This is your code from your tutorial:
      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);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      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();
      }
      }

  • @Senshiii99
    @Senshiii99 8 лет назад

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

  • @sraj7284
    @sraj7284 9 лет назад

    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?

    • @EJMedia1
      @EJMedia1  9 лет назад

      S Raj Oh ok I see what you asking - I am trying to remember this code haha its been a while - ok here is what is going on -> Gui () is the constructor so that is why you dont need the object in front of it - in other words we are saying hey do these things when we build the object and so those are being used to create the screen. Since GUI () is extended off of JFRAME the methods are from JFRAME ... now actually a lot of work will be done in the constructors .. when you build the object

  • @eobardthawne9352
    @eobardthawne9352 7 лет назад

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

  • @sakaabdulrahman1702
    @sakaabdulrahman1702 9 лет назад

    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

    • @EJMedia1
      @EJMedia1  9 лет назад

      Saka Abdulrahman Try this code:
      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);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      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();
      }
      }

  • @manicho3781
    @manicho3781 10 лет назад

    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

    • @EJMedia1
      @EJMedia1  10 лет назад

      Mani Cho For the beginning series there are about 40 left

    • @manicho3781
      @manicho3781 10 лет назад

      ...
      This is just the beginning series? I thought if I follow this path I'll learn most of java at least.

    • @EJMedia1
      @EJMedia1  10 лет назад

      Mani Cho Well I have looked at what some other you tube publishers call intermediate is not my intermediate ... I am introducing concepts in my beginner series that they introduce much later on ... so at the end of this you will know java pretty darn well

    • @manicho3781
      @manicho3781 10 лет назад

      Oh, thanks! I'll continue to watch your videos! :D

    • @EJMedia1
      @EJMedia1  10 лет назад

      Mani Cho The hope is by the end you will be able to read code and understand how it works ... you also should start to be able to write some code from scratch or edit existing code - however you want to do it - editing existing code is always easier than writing from scratch -

  • @tingtingy
    @tingtingy 10 лет назад

    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

    • @EJMedia1
      @EJMedia1  10 лет назад

      John Barry That's strange I will test it out tonight - did you use the code that I pasted into the video at all? Just curious

    • @EJMedia1
      @EJMedia1  10 лет назад

      John Barry Just curious if you used the code I pasted into the message board of the video? thanks

    • @tingtingy
      @tingtingy 10 лет назад

      Hey yeh I tried my own version which didnt work. So I copied and pasted the one you commented and again same thing (frame shows up with no content) but moving the setVisible part to the end worked! it is strange why it didn't work for me in the beginning. Cheers

  • @karimaali4821
    @karimaali4821 9 лет назад

    sir i have 2 question
    the code (keyText.addKeyListener(this)) what do it mean
    second
    when i call the constructor in the main method how it read all the class
    thanks

    • @EJMedia1
      @EJMedia1  9 лет назад +1

      karima ali Hello - the (keyText.addKeyListener(this)) line -- this adds the listener
      to the component so that it can listen to key events .. in this case the component is the text field which is our object keyText ................ the main method creates the object go - and the constructor forms the window and sets up the listener and then its done - now nothing else happens until a keyboard event gets triggered then the method keyPressed gets executed

    • @karimaali4821
      @karimaali4821 9 лет назад

      EJ Media thanks :))

  • @SorenBagley
    @SorenBagley 8 лет назад

    i keep getting an error message at the line---- keyText.addKeyListener(this);----error 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?

    • @EJMedia1
      @EJMedia1  8 лет назад

      +Soren Bagley Can you paste the code in the message board so I can try it? thx

    • @SorenBagley
      @SorenBagley 8 лет назад

      +EJ Media Sure thing. Thanks for the quick response
      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");

      Gui(){
      keyText.addKeyListener(this);
      setSize(400,400);
      setVisible(true);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      BorderLayout layout = new BorderLayout();
      setLayout(layout);
      add(keyLabel, BorderLayout.NORTH);
      add (keyText, BorderLayout.CENTER);
      }
      public void keyTyped (KeyEvent e){
      }
      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 button");
      }
      }
      public void keyReleased (KeyEvent e){
      }
      public static void main(String[] args) {
      Gui go = new Gui();
      }
      }

    • @EJMedia1
      @EJMedia1  8 лет назад

      +Soren Bagley I will take a look at it tonight and see what I can find out

    • @SorenBagley
      @SorenBagley 8 лет назад

      +EJ Media thanks a million

    • @tanu3873
      @tanu3873 8 лет назад

      +Quinten Bredeveldt Can you explain why we need to implement KeyListener in the first place? I thought interfaces just say you need to implement these methods? So if I already typed those methods without typing 'implements KeyListener', there shouldn;t be a problem, right?

  • @phucoan2437
    @phucoan2437 3 года назад

    sir, i need u show class main pls

  • @voizer7660
    @voizer7660 8 лет назад

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

    • @EJMedia1
      @EJMedia1  8 лет назад

      i want to add some stuff on JavaFX and then tie that into a game design tutorial

    • @voizer7660
      @voizer7660 8 лет назад

      Ok.

  • @stef1896
    @stef1896 8 лет назад +2

    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.

    • @EJMedia1
      @EJMedia1  8 лет назад +1

      +stef wth This was the first series I made so I made the mistake of not saving some of the code - but I am in the process of trying to get the code uploaded for these videos. Every video that I post these days always has the code in the description.

    • @robloxprisonlife3390
      @robloxprisonlife3390 5 лет назад

      ITS YOUR FAULT

  • @mollytaylor8661
    @mollytaylor8661 6 лет назад

    I typed up this lesson's code and posted it on github here:
    github.com/Taylomol001/Java-Tutorial-for-Beginners---43---GUI---Setting-up-KeyListener-Keyboard-Events-JLabel-and-Textbox

  • @jorgeramos637
    @jorgeramos637 7 лет назад

    how come keycode is an int and not a char?

    • @michaelong348
      @michaelong348 7 лет назад +1

      because not all keys have a char value

  • @TheSkyGamez
    @TheSkyGamez 5 лет назад

    it doesnt fire at all, with no errors

  • @MrAirPork1
    @MrAirPork1 9 лет назад

    Why is there a need for ( extends JFrame ) in the vid? The previous vids didn't inherit JFrame yet was able to use it.
    And what does setLayout(layout) mean?

    • @EJMedia1
      @EJMedia1  9 лет назад

      MrAirPork1 I don't have the code in front of me for 42 but I sort of remember I just needed to create an object for the method - we didnt need to extend it at the class level in that lesson

    • @MrAirPork1
      @MrAirPork1 9 лет назад

      EJ Media Hmm your point made me realised that in 42 you made an object to access the methods. How come no object is needed here to access the JFrame methods?

    • @EJMedia1
      @EJMedia1  9 лет назад +1

      MrAirPork1 There are different ways you can do things - in this case I wanted to access JFrame from the constructor - so at the time the object was created