如何使JFrame达到一定的大小(不包括边框)? [英] How do I make a JFrame a certain size, not including the border?

查看:80
本文介绍了如何使JFrame达到一定的大小(不包括边框)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用setBounds设置为特定大小的JFrame.但是,这会使窗口(包括边界)达到这样的大小(事后看来是完全有意义的).但是我想要的是窗口的大小,例如800x600 plus 边框.这很重要,因为我正在从JFrame的BufferStategy中绘制到Graphics对象,但是当使用小于20的y值时,我一直在标题栏下绘制.我想象到了不同的操作系统(甚至不同的操作系统设置)也可以有不同的厚度边框.我以为之后才把边界钉在窗户上,但是事实并非如此.

I have a JFrame that I've set to a certain size, using setBounds. However, that makes the window, including the borders, that size (which in hindsight makes complete sense). But what I want is for the size of the window it be, say, 800x600 plus the borders. This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame, but I've been drawing underneath the title bar when using a y value of less than about 20. I imagine different OSs (or even different OS settings) can have different thickness borders, too. I thought the borders were just tacked on a window afterwards, but this doesn't seem to be the case.

那么,无论边框的厚度如何,如何使边框内的空间一定大小?另外,为了使我的生活更轻松,如何使点0、0成为框架可见内容的左上角?

So, how do I make the space inside the borders a certain size, regardless of the thickness of the borders? Also, to make my life easier, how do I make point 0, 0 be the top left corner of the viewable contents of the frame?

顺便说一句,使用setUndecorated会带来它自己的问题,因此我暂时不尝试这样做.

By the way, using setUndecorated presents it's own problems, so I'm not trying that at the moment.

推荐答案

这很重要,因为我是从JFrame的BufferStategy绘制到Graphics对象的,

This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame,

为什么要使用BufferStrategy.那是旧的AWT代码.默认情况下,Swing是双缓冲的.

Why are you using a BufferStrategy. That is old AWT code. Swing is double buffered by default.

在Swing中进行自定义绘制时,应扩展JPanel(或JComponent),然后重写paintComponent()方法.您将此组件添加到框架的内容窗格.然后,如果您遵循kleopatra的建议,就不会有问题.

When doing custom painting in Swing you should extend JPanel (or JComponent) and then override the paintComponent() method. You add this component to the content pane of the frame. Then if you follow kleopatra's advice you won't have a problem.

请参阅Swing教程中自定义绘画的部分有关更多信息和示例.

See the section from the Swing tutorial on Custom Painting for more information and examples.

这篇关于如何使JFrame达到一定的大小(不包括边框)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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