简体   繁体   English

画布的JFrame像素精度

[英]JFrame pixel precision for canvas

Hey so I'm implementing a Canvas in a JFrame object, however there's a slight annoyance: 嘿所以我在JFrame对象中实现了Canvas,但是有一点点烦恼:

If I specify the frame to be a certain dimension, then the title bar is included in the vertical size. 如果我将帧指定为某个维度,则标题栏将包含在垂直大小中。 Similarly for the left edge of the window being included with the specified size. 类似地,窗口的左边缘包含指定的大小。 I used absolute positioning to determine the pixel count for the title bar and just made my JFrame that quantity of pixels larger, however I somehow doubt this is platform independent. 我使用绝对定位来确定标题栏的像素数,并让我的JFrame使像素数量更大,但我不知道这是否与平台无关。 Is there a proper way to get/set the quantity of pixels that can appear inside the actually usable contents of a JFrame? 是否有正确的方法来获取/设置可出现在JFrame实际可用内容中的像素数量?

Usually you place your "canvas" (which should not be a java.awt.Canvas , but preferably a javax.swing.JPanel ) into the content pane of the frame. 通常将“canvas”(不应该是java.awt.Canvas ,但最好是javax.swing.JPanel )放入框架的内容窗格中。 When you assign a preferred size to this component, and afterwards call pack() on the frame, then the frame will have the right size to obey the preferred size of the component: 当您为此组件指定首选大小,然后在框架上调用pack()时,框架将具有正确的大小以遵守组件的首选大小:

canvas = new MyClassExtendingJPanel();
canvas.setPreferredSize(new Dimension(123,456));
frame.getContentPane().add(canvas);
...
// At the end:
frame.pack(); 
frame.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM