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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:48:51+00:00 2026-05-26T21:48:51+00:00

This is my JFrame code: public static int width = 800; public static int

  • 0

This is my JFrame code:

public static int width = 800;
public static int height = 600;

public static void main(String[]args){
    JFrame frame= new JFrame("RETRO");
    frame.add(new Screen());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width,height);
    frame.setVisible(true);
    frame.setResizable(false);
}

Basically when I want something to move to the edge of the screen, I have to add extra pixels for it work (I’m guessing because it includes the frame itself instead of just the display size? However the origins work fine (x=0, y=0)). Example:

public double getX(){
    if(x<0)
        x=0;
    if(x+getImage().getWidth(null)>Game.width-6)
        x=Game.width-6-getImage().getWidth(null);
    return x;
}

public double getY(){
    if(y<0)
        y=0;
    if(y+getImage().getHeight(null)>Game.height-26)
        y=Game.height-26-getImage().getHeight(null);
    return y;
}

Is there a way around this? I don’t think the JFrame would be the same size on everyone’s computer, not to mention the guesswork. Rather have it much neater and flexible by using an exiting variable from the JFrame component. Does there exist something like a frame.getDisplayWidth and Height function?

  • 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-05-26T21:48:52+00:00Added an answer on May 26, 2026 at 9:48 pm
    • Don’t set the size of the frame, set the preferred size of the contents.
    • Pack the frame
    • Get co-ordinates according to the position in the content

    How do you set a ‘preferred size’ for the contents? I’m using a Screen class (extended JPanel) for rendering.

    screen.setPreferredSize(new Dimension (600,400));
    frame.setContentPane(screen);
    frame.pack();
    // frame will now be the size it needs to display the contents 
    // and the frame's own decorations (title bar etc.)
    // ..now add a nice tweak.
    frame.setMinimumSize(frame.getSize());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I make a JFrame like this public static void main(String[] args) { new
import java.awt.*; import javax.swing.*; import java.awt.geom.*; public class Box { public static void main(String[]
I have this code: public class Window extends JFrame { public Window(){ ... JButton
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
There are number of problems with this code public class LineEx extends JFrame implements
import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; @SuppressWarnings(serial) public class Main extends JFrame
I have a JFrame. This JFrame contains a JButton. I click the JButton and
This one's a tough one - I have a JFrame that generates JTextFields. When
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have write this code for displaying some set of colors from panel: import

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.