Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:53:09+00:00 2026-06-05T09:53:09+00:00

Basically, I’m trying to move a JButton (or even a JLabel ) inside a

  • 0

Basically, I’m trying to move a JButton (or even a JLabel) inside a frame using the arrow keys.
I’ve successfully figured out the part to detect the arrow key presses. Now all i want to do is to change the position of the button (or label) depending on the key pressed.

E.G. when I press the “UP” key, the button/label should move up by say 5 pixels. I couldn’t find the property to change the position of the button.

Do I need to put button/label inside the panel or something like that?

Please help, its not a homework, just curious 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:53:11+00:00Added an answer on June 5, 2026 at 9:53 am

    How I would do it is to use the Graphics instead of a Panel. So the bit of code that draws it could be:

        public class panel extends JPanel{
    public int X = 50; //Starting x
    public int Y = 50; //Starting y
    public panel(){
    
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        g.drawString("Hello this will be moved", X, Y);//<--- This draws the text
    }
    

    }

    This bit of code will draw the text and repeatedly draw it. All you have to do is in the Key Listener or what ever was used, do;

      X++;
    

    or:

      Y++;
    

    That will add 1 everytime. So when you press the key it is moved one x pixel or y pixel or back or forth, depending on the ++ or –. Then add this to the JFrame and it will draw it on screen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I'm using python to print out some information in the terminal and some
Basically, what I'm trying to create is a page of div tags, each has
Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.
Basically i'm trying to build a comment system. The user looks at a photo
Basically, what I am trying to do is to find last section of PE
Basically I have a single element inside of an xml file where I store
Basically i've a table for my users (it's nightclub website) and now i'm trying
Basically I am trying to restart a service from a php web page. Here
Basically, I'm trying to tap into the Soap pipeline in .NET 2.0 - I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.