如何动态更改 JFrame 大小
我有一个简单的绘图程序,我使用以下代码设置 Jframe 的大小:
frame.setSize(900, 700);
但是,当用户更改窗口的大小时,要绘制的白色区域仍保持相同的大小,因此,即使用户放大窗口,也无法绘制。
http://forum.codecall.net/java-tutorials /31180-java-mini-paint-program.html 这是我开始的地方。我扩展了类“PadDraw”并将大部分代码写入那里,在我的其他java文件中,我只创建一个框架,然后创建“PadDraw”对象,我创建了一个容器,然后将对象添加到框架的容器中:content.添加(drawPad,BorderLayout.CENTER);
我改变了我的代码:
public class PadDraw extends JComponent implements ActionListener, ItemListener, ComponentListener{
public synchronized void addComponentListener(ComponentListener l) {};
. 。 。 。
我添加了未实现的方法,“componentResized”是:
public void componentResized(ComponentEvent arg0) {
System.out.println("Changed ????");
}
但是当我更改窗口的大小时,没有任何反应。
一种想法:当我将 componentlistener 添加到其他文件而不是 drawPad 时,componentResized 方法标志,但因为我在此事件发生之前创建了 drawPad 对象,所以我无法更改大小 =/
谢谢
I have a simple drawing program, I set my Jframe's size with the following code:
frame.setSize(900, 700);
However, when user change the size of the window, white area to draw, still remain same size therefore, user is not able to draw eventhough he enlarge the window.
http://forum.codecall.net/java-tutorials/31180-java-mini-paint-program.html this is where I start from. I extended class "PadDraw" and wrote most of my code to there, in my other java file, I only create a frame, then I create "PadDraw" object, I crated a container and then added object to the frame's container: content.add(drawPad, BorderLayout.CENTER);
I changed my code:
public class PadDraw extends JComponent implements ActionListener, ItemListener, ComponentListener{
public synchronized void addComponentListener(ComponentListener l) {};
.
.
.
.
And I added unimplemented methods, and the "componentResized" is:
public void componentResized(ComponentEvent arg0) {
System.out.println("Changed ????");
}
But when I changed the window's size, nothing happens.
One thougt: When I added componentlistener to my other file instead of drawPad, componentResized method flags, but because I have created drawPad object before this event occured, I can't change the size =/
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望您不是直接在
JFrame
上绘图,而是在已添加到JFrame
的JPanel
或JComponent
中绘图> 的内容窗格。如果已添加BorderLayout.CENTER
,那么它应该随着JFrame
扩展和收缩而扩展和收缩。如果您在 BufferedImage 上绘图,事情会变得有点棘手,因为您必须使用 ComponentListener 来查看何时保存 BufferedImage 的组件code> 更改大小,当更改大小时,更改 BufferedImage 的大小,而不会丢失已有的绘图。如果此答案不够有帮助,请考虑向我们提供有关您当前问题和当前计划的更具体信息。
在您链接到的程序中,您正在一个 Image 对象上绘图,该对象的大小是在第一次调用 Paint 时设置的。您需要做的是将
ComponentListener
添加到 PadDraw 对象的构造函数中,以及在ComponentListener
的componentResized
方法中,使用PadDraw
的新高度和宽度调整图像大小。但是,只有当新尺寸大于旧尺寸时,我才会这样做。然后,在将图像变量 = 设置为新图像之前,您还需要在新图像上重绘旧图像。然后调用重画。Hopefully you're not drawing directly on the
JFrame
but rather in aJPanel
orJComponent
that's been added to theJFrame
's contentPane. If it has been addedBorderLayout.CENTER
, then it should expand and contract as theJFrame
expands and contracts. Things get a little trickier if you're drawing on aBufferedImage
as you'll have to use aComponentListener
to see when the component that holds theBufferedImage
changes size, and when it does, change the size of theBufferedImage
without losing the drawing that's already there.If this answer is not helpful enough, consider giving us more specific information about your current problem and your current program.
In the program that you link to, you're drawing on an Image object whose size is set the first time paint is called. What you'll want to do is to add a
ComponentListener
to the PadDraw object in its own constructer, and in theComponentListener
'scomponentResized
method, resize your image using thePadDraw
's new height and width. I would only do this however if the new size is larger than the old size. You would also then want to redraw the old image on the new before setting the image variable = to the new image. Then call repaint.