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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:11:05+00:00 2026-06-08T04:11:05+00:00

I have noticed that running the program I’m listing below sometimes produces an unwanted

  • 0

I have noticed that running the program I’m listing below sometimes produces an unwanted effect.

EDIT: I’ve simplified the code to make things look clear. I’m drawing a String which prints out the current component size. I’ve overriden the getPrefferedSize() method in the Component class and set width and height to 640 x 512 respectively. However, I’m still getting different results after running the program: 640 x 512 and 650 x 522. Weird thing is removing the frame.setResizable(false) line fixes things. But I want the window to be resizable

import java.awt.*;

import javax.swing.*;

public class DrawTest
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                DrawFrame frame = new DrawFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}


class DrawFrame extends JFrame
{

    private static final long serialVersionUID = 1L;
    public DrawFrame()
    {
        setTitle("DrawTest");
        setLocationByPlatform(true);
        Container contentPane = getContentPane();
        DrawComponent component = new DrawComponent();
        contentPane.add(component);
    }

}

class DrawComponent extends JComponent
{
    private static final long serialVersionUID = 1L;
    public Dimension getPreferredSize() {
        return new Dimension(640, 512);
    }

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;         
        String msg = getWidth() + " x " + getHeight();      
        g2.setPaint(Color.BLUE);
        g2.drawString(msg, getWidth()/2, getHeight()/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-08T04:11:06+00:00Added an answer on June 8, 2026 at 4:11 am

    The call to setLocationByPlatform() must follow pack() and precede setVisible(), or the geometry will be wrong. Absent a reason to subclass JFrame, I have elided DrawFrame is the example below. Note the use of FontMetrics to keep the text centered as the enclosing container is resized. The approach is handy for learning layouts.

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    
    public class DrawTest {
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JFrame frame = new JFrame("DrawTest");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new DrawComponent());
                    frame.pack();
                    frame.setLocationByPlatform(true);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class DrawComponent extends JComponent {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(320, 240);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            String msg = getWidth() + " x " + getHeight();
            g2.setPaint(Color.BLUE);
            int w = (getWidth() - g.getFontMetrics().stringWidth(msg)) / 2;
            g2.drawString(msg, w, getHeight() / 2);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have noticed that my application's Document folder is sometimes different. The workflow was
Suppose I have a program running that periodically adds information to a .CSV file.
We have noticed that component publishing takes a long time. The component we are
I have noticed that if you add a unobtrusive link in a refreshable partial
I have noticed that deleted branches in svn is not preserved in git. I
I have noticed that ie tends to put the thread for a tab to
I have noticed that it is possible to define a custom class and then
I have noticed that most Objective-C coders never use the self->ivar syntax when accessing
I have noticed that doing actions like implementing an interface, that a small rectangle
I have noticed that the Windows Vista/7 dialogs look MUCH better than just a

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.