Java Canvas 白边
所以我想制作一个基于 Canvas 的 Java 应用程序。我已将主类扩展为 Canvas 并在其构造函数中调整其大小。
public CanvasApp() {
Dimension size = new Dimension(640, 480);
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
在 main 函数中,我为它创建了一个框架,如下所示:
CanvasApp cnv = new CanvasApp();
JFrame frame = new JFrame("");
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(null); //I've tried this
panel.setSize(640,480); //but still doesn't work =(
panel.add(cnv, BorderLayout.CENTER);
frame.setContentPane(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
但内容窗格的大小显示为 650x490。这是为什么呢?
我也附上了一张照片。
So I'd like to make a Canvas
based Java application. I've extended my main class to Canvas
and I size it in it's constructor.
public CanvasApp() {
Dimension size = new Dimension(640, 480);
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
}
and in the main function, I make a frame for it, like this:
CanvasApp cnv = new CanvasApp();
JFrame frame = new JFrame("");
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(null); //I've tried this
panel.setSize(640,480); //but still doesn't work =(
panel.add(cnv, BorderLayout.CENTER);
frame.setContentPane(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
But the content pane appears 650x490 in size. Why is this?
I've attached a picture too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 JFrame 周围有一个 5 像素的边框,所以查看计算机屏幕上的任何框架,您都会注意到一种浮雕边框 - 该边框的宽度为 5 像素,在高度和宽度上添加 10 像素。您仅将面板指定为 640 x 480 并将其放入框架内 - 然后框架将其自己的边框添加到该面板上。
Because a JFrame has a 5px border around it, look at any frame on your computer screen you'll notice a kind of embossed border - that border is 5 px in width adding 10 pixels onto height and width. You've only assigned the panel to 640 x 480 and plonked it inside the frame - the frame then adds it's own border onto that.
这可能是因为每个 JComponen 都有一个边框,您将 CanvasApp 放入 JPanel 中,并将 JPanel 本身放入 JFrame 中。这可能就是为什么你最终会得到更大尺寸的原因。
有关如何使用边框的信息,请参阅 Oracle 网站:http://docs。 oracle.com/javase/tutorial/uiswing/components/border.html
Thats probably because every JComponen have a border, you put your CanvasApp inside JPanel and JPanel itself into JFrame. That is probably the reason why you get bigger dimensions at the end.
See the oracle website on how to use borders here: http://docs.oracle.com/javase/tutorial/uiswing/components/border.html