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 9314581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:10:09+00:00 2026-06-19T02:10:09+00:00

I have created a simple compass program that moves the GUI box around the

  • 0

I have created a simple compass program that moves the GUI box around the screen and I am now progressing on to adding NE, NW etc.

This involves using JPanels as this allows multiple objects. My issue is that I’ve made 9 separate panels for the separate buttons but I have no idea how to add them to the JFrame as everything I do doesn’t seem to work.

Any ideas would be appreciated.

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

public class MovingCompassExtraJFrame extends JFrame implements ActionListener {

    private JButton north;
    private JButton east;
    private JButton west; 
    private JButton south;
    private JButton center;
    private JButton northEast;
    private JButton northWest;
    private JButton southEast;
    private JButton southWest;
    int screenHeight;
    int screenWidth;
    int height;
    int width;

    public MovingCompassExtraJFrame() 
   {
        super();

            width = 400;
            height = 300;

            setSize(width, height);
            setLocation(200, 100);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            getContentPane().setBackground(Color.RED); 
            getContentPane().setLayout(new GridLayout(3,3));

            setupNEPanel();
            setupNPanel();
            setupNWPanel();
            setupWPanel();
            setupCPanel();
            setupEPanel();
            setupSWPanel();
            setupSPanel();
            setupSEPanel();

            setVisible(true);

   }

   private JPanel setupNEPanel()
      { 
            northEast = new JButton("Move to North East");

            JPanel northEP = new JPanel();

            getContentPane().add(northEast);
            east.addActionListener(this);

            return northEP;
      }

    private JPanel setupNPanel()
      {
         north = new JButton("Move to North");

         JPanel northP = new JPanel();

         getContentPane().add(north);
         north.addActionListener(this);

         return northP;
      }

    private JPanel setupNWPanel()
      {
         northWest = new JButton("Move to North West");

         JPanel northWP = new JPanel();

         getContentPane().add(northWest); 
         west.addActionListener(this);

         return northWP;
      }

    private JPanel setupWPanel()
      {

            west = new JButton("Move to West");

            JPanel westP = new JPanel();

            getContentPane().add(west); 
            west.addActionListener(this);

            return westP;
      }

    private JPanel setupCPanel()
      {
            center = new JButton("Move to Center");

            JPanel centerP = new JPanel();

            getContentPane().add(center);
            center.addActionListener(this);

            return centerP;
      }

    private JPanel setupEPanel()
      {
            east = new JButton("Move to East");

            JPanel eastP = new JPanel();

            getContentPane().add(east);
            east.addActionListener(this);

            return eastP;

      }

    private JPanel setupSEPanel()
      {
            southEast = new JButton("Move to South East");

            JPanel southEP = new JPanel();

            getContentPane().add(southEast);
            east.addActionListener(this);

            return southEP;
      }

    private JPanel setupSPanel()
      {
            south = new JButton("Move to South");

            JPanel southP = new JPanel();

            getContentPane().add(south);
            south.addActionListener(this);

            return southP;
      }

    private JPanel setupSWPanel()
      {
            southWest = new JButton("Move to South West");

            JPanel southWP = new JPanel();

            getContentPane().add(southWest); 
            west.addActionListener(this);

            return southWP;
      }

    public void actionPerformed(ActionEvent e)
    {
        screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;


        if (e.getSource() == north)
        {
            this.setLocation((screenWidth - width)/ 2, 0);
        }
        else if (e.getSource() == south)
        {
            this.setLocation((screenWidth - width)/ 2, (screenHeight-height)- 30);
        }
        else if (e.getSource() == east)
        {
            this.setLocation(screenWidth - width , (screenHeight-height)/2);
        }
        else if (e.getSource() == west)
        {
           this.setLocation(0, (screenHeight-height)/2); 
        }
        else if (e.getSource() == center)
        {
            this.setLocation((screenWidth-width)/2, (screenHeight - height)/2);
        }
  }
}
  • 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-19T02:10:10+00:00Added an answer on June 19, 2026 at 2:10 am
    JFrame frame = new JFrame ("Compass");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout(3, 3));
    frame.add (new JButton ("NW"));
    frame.add (new JButton ("N"));
    frame.add (new JButton ("NE"));
    frame.add (new JButton ("W"));
    frame.add (new JButton (" "));
    frame.add (new JButton ("E"));
    frame.add (new JButton ("SW"));
    frame.add (new JButton ("S"));
    frame.add (new JButton ("SE"));
    frame.pack();
    frame.setVisible(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have created simple Ajax enabled contact forms before that have around 12 fields -
I have created a simple text input box that will be used to fetch
I have created simple SSL client server program and in that program I am
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I have created a simple WordPress plugin that automatically sets my new sites up
I have created simple application in windows sharepoint services. Now I want to deploy
I have created a simple chat server that is driven by client polling. Clients
I have created a simple command line tool that outputs hello world. This is
I have created a simple form that inserts/updates/deletes a values for Northwind Customers. Everything
I have created a simple heroku app that takes a url as the argument,

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.