Java JFrame setSize无法正常工作 [英] Java JFrame setSize doesn`t work properly

查看:86
本文介绍了Java JFrame setSize无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个尺寸为500x500像素的JFrame. 我将背景设为蓝色,并在右下角从(490,490)到(500,500)添加一个红色方块.

图片:

我在屏幕上看不到红色方块. 我将框架从不可调整大小切换为可调整大小,如果将窗口放大,则红点在那里. 框架大小是否与应用程序的窗口大小相同? 如何使应用程序的窗口恰好是500x500?

解决方案

框架是整个窗口的大小,包括操作系统所需的标题栏.在JFrameJPanel中绘制内容时,如果JPanel从标题栏的下方开始,则(0,0)坐标位于左上角.听起来您的标题栏比10像素高,所以490作为y分量实际上不在窗口内,因为JPanel的可见高度是windowHeight - titleBarHeight.

I am making a JFrame with the size of 500x500 pixels. I make a blue background and add a red square in the right-bottom corner from (490,490) to (500,500).

Image:

I don't see the red square on the screen. I switched the frame from not resizable to resizable and if I make the window larger the red dot is there. Is the frame size the same as application's window size? How can I make the application's window to be the exactly 500x500?

解决方案

The frame is the size of the entire window, including the title bar required by the OS. When drawing things in the JPanel in the JFrame, the (0, 0) coordinate is in the top left corner if the JPanel, which begins just below the title bar. It sounds like your title bar is taller than 10 pixels, so 490 as a y component is actually off the window, since the visible height of the JPanel is windowHeight - titleBarHeight.

这篇关于Java JFrame setSize无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆