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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:33:55+00:00 2026-06-14T15:33:55+00:00

I get a JFrame and i want to display a JLabel with a border

  • 0

I get a JFrame and i want to display a JLabel with a border in it with a padding of maybe 50px. When i set the size of the JFrame to 750, 750, and the size of the JLabel to 650, 650 and the location to 50, 50, it display it strange… Here’s my code:

public class GUI {

    /**
     * Declarate all 
     */
    public int height = 750;
    public int width = 750;

    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width / 2) - (width / 2); // Center horizontally.
    int y = (screen.height / 2) - (height / 2); // Center vertically.

    /**
     * Create the GUI
     */
    JFrame frame = new JFrame();
    Border border = LineBorder.createBlackLineBorder();
    JLabel label = new JLabel();    

    public GUI(){
        label.setBorder(border);
        label.setSize(700, 700);
        label.setLocation(0, 0);

        frame.getContentPane().setLayout(null);
        frame.add(label);
    }

    public void createGUI() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(x,y,width,height);
        frame.setVisible(true);
    }

}

So I think the title-bar at the top is also include in the size. In Graphics you can use getInsets(). Now is there anything like that for Swing / JFrame?

  • 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-14T15:33:56+00:00Added an answer on June 14, 2026 at 3:33 pm

    First get the pixels trimmed out by the frame.

    int reqWidth = reqHeight = 750;
    
    // first set the size
    frame.setSize(reqWidth, reqHeight);
    
    // This is not the actual-sized frame. get the actual size
    Dimension actualSize = frame.getContentPane().getSize();
    
    int extraW = reqWidth - actualSize.width;
    int extraH = reqHeight - actualSize.height;
    
    // Now set the size.
    frame.setSize(reqWidth + extraW, reqHeight + extraH);
    

    An alternate simpler way. The previous works but this is recommended.

    frame.getContentPane().setPreferredSize(750, 750);
    frame.pack();
    

    Hope this helps.

    EDIT:

    Add this in your constructor before adding components to the frame. and to set it in the middle, use

    frame.setLocationRelativeTo(null);
    

    This will center the window on the screen.

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

Sidebar

Related Questions

I want to get the present size of my Jframe whenever i click on
I want to animate a JFrame to become half-size when i press a button
I've a maven project where there is a JFrame and I want to set
I want to get the data from my textfield and set it to int
I am trying to get a custom icon onto my JFrame . I have
I get the following error even when my JAVA_HOME is set correctly. C:\workspace-sts-2.8.0.RELEASE\JBClient\target>echo %JAVA_HOME%
I'm writing a program where I have a JFrame and I want to remove
basically I want my JFrame to become a completely new JFrame object when an
i want to get the value from the txt field val1 and val2 and
I want to make the map infinite with tiles but when I get in

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.