Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Complete newbie keybinding

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Complete newbie keybinding

    So, I'm really new to Java as a whole. Started a class on it 2 months ago now, but I have gotten really into it. I've started doing graphics based things, but most of my learning is without a specific teacher, just trial and error. This has led to some... gaps... in my knowledge. Mainly in terminology. Every time I try and use an online guide, I get hopelessly turned around, so I'm here asking humans who can probably help explain it better for help.

    Here's what I want to do:
    I know how to make a label move when I click a button, I want it to move when I press keys. Specifically, WASD and arrows.

    Like I said, I have no idea what most of the terminology means, or even if I have this in the right section, so write this as if you were explaining it to an idiot.

    Thanks in advance
    -Peperono


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Complete newbie keybinding

    What results did you get when you used Google?
    Improving the world one idiot at a time!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,370
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Complete newbie keybinding

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Complete newbie keybinding

    Yeah, I messed around with the stuff I got off Google, I wasnt sure exactly what I was looking for though, so I kept coming up empty. Those links were great though! Thanks.

    So... It's mostly working. Just one bug. It wont work with just the keys, it also requires alt. As in, Alt+A to move left.

    I'm not sure how much of my code ill need to post, so ill try and limit it to the relevant parts.

    btnWest = new JButton("West");
    btnWest.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0){
    lblBoat.setBounds((x = x - 30), y, 50, 40);
    location.setRect (x, y, 50, 40);
    if (x <= 0){
    x = 0;
    location.setRect (x, y, 50, 40);
    lblBoat.setBounds(x, y, 50, 40);
    }
    }
    });
    btnWest.setMnemonic(KeyEvent.VK_A);
    btnWest.setBounds(0, 25, 70, 25);
    contentPane.add(btnWest);


    So thats one of my 4 movement buttons. Any advice/help for fixing my alt problem?

    Edit: Wow, it retained none of my tabbing, is there a way to put quote tags on too?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,370
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Complete newbie keybinding

    Quote Originally Posted by Peperono View Post
    Yeah, I messed around with the stuff I got off Google, I wasnt sure exactly what I was looking for though, so I kept coming up empty. Those links were great though! Thanks.

    So... It's mostly working. Just one bug. It wont work with just the keys, it also requires alt. As in, Alt+A to move left.
    What you're talking about is called a mnemonic, which is neither key bindings nor a KeyListener. Those do require alt and a key, and they're for navigating menus and controls. For example, in firefox, alt+F opens up the file menu.


    Quote Originally Posted by Peperono View Post
    I'm not sure how much of my code ill need to post, so ill try and limit it to the relevant parts.
    When in doubt, post an SSCCE.


    Quote Originally Posted by Peperono View Post
    So thats one of my 4 movement buttons. Any advice/help for fixing my alt problem?
    Don't use a mnemonic. Use key bindings or a KeyListener instead.

    Quote Originally Posted by Peperono View Post
    Edit: Wow, it retained none of my tabbing, is there a way to put quote tags on too?
    Use the highlight tags. See the link in my signature, or quote another post with them to see how they did it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. A little help need to complete my tutorial question
    By hengchuen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 07:51 AM
  2. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 02:17 PM
  3. How to complete code
    By Shay in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2010, 01:59 PM
  4. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2010, 09:07 AM
  5. How to delete and add elements to an array dynamically?
    By k_w_r2007 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2009, 11:31 AM