Java 中 Canvas 不随窗口调整大小?
我有一个带有 Canvas 的 jFrame 。 当我在 Windows XP/Vista 中运行程序并调整窗口大小时,画布会随着窗口按比例调整大小。
然而,在 Ubuntu Linux 中,当我编译相同的 java 应用程序并调整窗口大小时,画布保持相同的大小。
我需要做什么才能使 Canvas 在 Windows 和 Linux 中随窗口调整大小? 差异是怎么回事?
Main.java
public class Main {
public static void main(String[] args)
{
JFrame frame = new JFrame("BallBounce");
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
BallCanvas ballCanvas = new BallCanvas();
frame.getContentPane().add(ballCanvas);
frame.getContentPane().add(controlPanel);
frame.pack();
frame.setVisible(true);
}
}
BallCanvas.java
public class BallCavnas extends Canvas {
public BallCanvas()
{
setPreferredSize(new Dimension(640, 400));
setIgnoreRepaint(true);
... various gui controls are wired up here
}
... rest of canvas code
}
编辑:我的源代码被压缩到这里,以防有人想看一下:
http://www.filedropper.com/ballbounce
我已经完成了 Dave Ray 提出的建议,它仍然没有调整画布的大小? 请记住,当我编译这个 java 程序并在 Windows 中运行它时,它会很好地调整大小。 只有在 Linux 中它才会对我这样做。 如果重要的话,我还运行 Java 6 Sun 1.6.0.10 JVM。
替代文本 http://img158.imageshack.us/img158/7642/screenshotww0.png< /a>
也许我的画布正在调整大小,因为我的 BufferStrategy/Graphics 没有调整大小?
编辑2:从屏幕截图来看,它肯定设置为中心:
frame.getContentPane().add(ballCanvas, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);
已解决
显然“画布”正在调整大小,但我正在使用它的缓冲区策略做了一些奇怪的事情,不允许调整IT大小。 我修好了它。 感谢大家!
I have a jFrame with a Canvas on it. When I run my program in Windows XP/Vista and resize the window the canvas resizes proportionately along with the window.
However, in Ubuntu linux, when I compile the same java application and resize my window, the Canvas stays the same size.
What do I need to do to make my Canvas resize with my window in both Windows and Linux? What is the deal with the discrepancy?
Main.java
public class Main {
public static void main(String[] args)
{
JFrame frame = new JFrame("BallBounce");
frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
BallCanvas ballCanvas = new BallCanvas();
frame.getContentPane().add(ballCanvas);
frame.getContentPane().add(controlPanel);
frame.pack();
frame.setVisible(true);
}
}
BallCanvas.java
public class BallCavnas extends Canvas {
public BallCanvas()
{
setPreferredSize(new Dimension(640, 400));
setIgnoreRepaint(true);
... various gui controls are wired up here
}
... rest of canvas code
}
Edit: My source code is zipped up here incase someone wants to take a look:
http://www.filedropper.com/ballbounce
I've done the suggestions made by Dave Ray, and it still isn't resizing the Canvas? Remember, it resizes for me fine when I compile this java program and run it in windows. Only in linux does it does this to me. I'm also running Java 6 Sun 1.6.0.10 JVM, if it matters.
alt text http://img158.imageshack.us/img158/7642/screenshotww0.png
Perhaps my canvas is resizing by my BufferStrategy/Graphics aren't resizing ?
Edit 2: From the screenshot, it is definitely set to CENTER:
frame.getContentPane().add(ballCanvas, BorderLayout.CENTER);
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);
Resolved
Apparently the "Canvas" was getting resized but I was doing something weird with it's buffer strategy that wasn't allowing IT to be resized. I fixed it. Thanks everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许布局管理器只是试图遵循您的首选尺寸。
我会:
A)删除首选只是为了看看会发生什么(无论如何不是一个很好的主意)
或
B)首先不使用canvas而是使用JComponent。 毕竟 Canvas 是 AWT 组件,而且我不太确定它们今天是如何工作的。 JComponent 是一个轻量级组件,由于您使用 JComponent 作为容器,它们会... mmhhh 一起工作得更好?
哎呀..我现在正在提供巫术编程建议。 最好开始工作。
C) 一直对我有用的东西。 通过逐步在我的代码中添加内容来进行一个小的概念证明。 从空画布开始,然后添加首选尺寸,然后依此类推。错误很可能出在绘制方法上:P
祝你好运。
:)
Perhaps, the layout manager is just attempting to honor your preferred size.
I would:
A) remove the preferred just to see what happens ( not a very good idea anyway )
or
B) not use canvas in first place but JComponent. After all Canvas is AWT component, and I not pretty sure how they work as today anyway. JComponent is a light weight component and since you're using a JComponent as container they would... mmhhh work better together?
Gee.. I'm giving voodoo programming suggestions now. Better get to work.
C) What have always worked for me. Make an small proof of concept, by adding step by step the stuff in my code. Start with empty canvas, then add the preffered size, then etc. etc. Chances are, the bug is on the paint method :P
Good luck.
:)
使用边框布局代替:
同时摆脱 setPreferredSize(),转而使用 frame.setSize() 来设置显示的初始大小。
Use a border layout instead:
Also get rid of setPreferredSize() in favor of frame.setSize() to set the initial size of the display.
另一个好的解决方案是将画布放入 JPanel 中,
就像我所做的
那样,在画布功能中,例如
。 这使得事情变得非常简单。 选择适合您需求的父面板。
Another good solution is put the canvas into an JPanel,
like i did
and in the canvas function something like
for example. This make it very simple. Take the parent Panel to fit to your needs.
如果你听从了戴夫的建议,尤其是。 将 ballCanvas 放在 CENTER 中,那么您必须设置 ballCanvas 的最大尺寸。
If you followed Dave's advice, esp. putting the ballCanvas in the CENTER, then you must be setting a maximum size for ballCanvas.