JFrame : Getting actual content size

2019-02-17 01:01发布

I have created a JFrame, and attempting to get it's size is giving an incorrect result, compared to what I expect.

I have determined that it is including the operating system borders around the edges, and the title bar.

How can I get/set the real size which I have available for rendering?

2条回答
Anthone
2楼-- · 2019-02-17 01:18

The size you are getting is the size of the content and the size of the insets. If you use Jcomponent.getInsets(), you can find the size of the content with simple subtraction.

查看更多
成全新的幸福
3楼-- · 2019-02-17 01:43

Your question is confusing: JPanels don't have title bars or window borders, they are abstract containers that are INSIDE JFrames. JFrames are the objects with title bars and window borders.

JPanel.getSize() should work as intended. However, for JFrames you'll want to use JFrame.getContentPane().getSize(), because getContentPane() returns the JPanel (which is the actual content area).

查看更多
登录 后发表回答