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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:39:14+00:00 2026-06-03T19:39:14+00:00

I am making a simple Java Swing GUI chessboard where the player can drag

  • 0

I am making a simple Java Swing GUI chessboard where the player can drag and drop pieces. The problem is that, because of the border around the frame (with the title on top, maximize/minimize/close buttons, etc), the coordinates are skewed off – (0, 0) is the upper-left-hand corner of the frame, that is, a little above the X button, but the GUI starts building itself right below the title bar, so the GUI doesn’t align with the coordinates, and things do not end up working the way they should. Additionally, when I set the size of the frame to, for instance, 100 x 100, the lower part and some of the right-hand part of my GUI is cut off because the frame doesn’t compensate for its border. When I run it as an applet, I don’t have this problem, but I don’t want to do that. How can I either get rid of that border around my frame window so I can just have the plain GUI, or have the coordinates set themselves up properly?

sscce:

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class class1 extends JFrame{
    public class1(){
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent evt){
                System.out.print(evt.getPoint());
            }
        });
    }

    public static void main(String[] args){
        class1 c = new class1();
        c.setTitle("Test");
        c.setSize(320, 320);
        c.setLocationRelativeTo(null);
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setVisible(true);
    }
}
  • 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-03T19:39:15+00:00Added an answer on June 3, 2026 at 7:39 pm

    It’s hard to know what is wrong with your code without the code, but I do know that if you go the easy way by using various layout managers, and let these managers do the laying out of components for you and the sizing of things as well, including calling pack() on the JFrame, usually things fall easily and well into place. So again, don’t set the size of anything, but rather let the components’ preferred sizes and the layout managers do this for you.

    If this advice doesn’t help, please give us more information and code, preferably an sscce, a small compilable and runnable program that doesn’t do anything other than demonstrate your problem.

    Edit: I am assuming that this is a Swing GUI. Please verify if this is so.

    Edit 2: One problem you’re having is that you’re setting the size of a JFrame not taking into account its “decorations” including the menu bar, the resize/maximize/close icon. Again, you shouldn’t be setting sizes directly, but if you must better override the getPreferredSize() method of the JPanel that holds your grid.

    Edit 3: For example:

    import java.awt.*;
    import javax.swing.*;
    
    public class Grid extends JPanel {
       public static final Color DARK_COLOR = Color.red.darker().darker().darker();
       public static final Color LIGHT_COLOR = Color.lightGray.brighter();
       public static final int SQUARE_SIDE = 60;
       private static final int ROW_COUNT = 8;
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(ROW_COUNT * SQUARE_SIDE, ROW_COUNT * SQUARE_SIDE);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (int i = 0; i < ROW_COUNT; i++) {
             for (int j = 0; j < ROW_COUNT; j++) {
                Color c = (i % 2 == j % 2) ? LIGHT_COLOR : DARK_COLOR;
                g.setColor(c);
                int x = i * SQUARE_SIDE;
                int y = j * SQUARE_SIDE;
                g.fillRect(x, y, SQUARE_SIDE, SQUARE_SIDE);
             }
          }
       }
    
       public Grid() {
          // TODO Auto-generated constructor stub
       }
    
       private static void createAndShowGui() {
          Grid mainPanel = new Grid();
    
          JFrame frame = new JFrame("Grid");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple game in Java using swing and am having problems with
I am making an application with Java Swing and i have a problem. I
I am making a simple Java program for class that is supposed to output
I'm making a simple Kakuro application in Java Swing and I have used JButton
I'm making a GUI using Java (who isn't?). I know the Swing Worker class
HTML can be used in Java swing GUI components , like JTextBox , JButton
Started making a simple GUI using netbeans and now I'm facing some problem. I
I'm making a simple drawing app on Android. I'm using the FingerPaint.java provided with
I am also new to Java and Android Development, been making a simple game
I'm making a very simple 2D RPG in Java. My goal is to do

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.