在 Netbeans 中获取计时器内的 JFrame 边界的问题
当我按下程序中的按钮时,我想将 JFrame 动画化为一半大小。我认为最简单的方法是将 JFrame 的当前边界放入计时器中,并在计时器运行时将边界 1 减 1。但是当我在 netbeans IDE 中声明一个新计时器时,它会看起来像这样。
Timer t = new Timer(5,new ActionListener() {
public void actionPerformed(ActionEvent e) {
//inside this I want to get my Jframe's bounds like this
// int width = this.getWidth();---------here,"this" means the Jframe
}
}
});
但问题是在这里“this”不是指 JFrame。而且我什至无法创建 JFrame 的新对象。因为它会给我另一个窗口。任何人都可以帮我解决这个问题吗?
I want to animate a JFrame to become half-size when i press a button in my programme. I think the easiest way is putting the current bounds of JFrame into a timer and decrease bounds 1 by 1 when the timer running.But when I declare a new timer in netbeans IDE it will looks like this.
Timer t = new Timer(5,new ActionListener() {
public void actionPerformed(ActionEvent e) {
//inside this I want to get my Jframe's bounds like this
// int width = this.getWidth();---------here,"this" means the Jframe
}
}
});
But the problem is in here "this" not refering to JFrame.And also I cant even create a new object of my JFrame.Because it will give me another window.Can anyone help me solve this problem ?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Foo
子类JFrame
。Try
where
Foo
subclassesJFrame
.因此,当您单击该按钮时,您可以访问该按钮。然后您可以使用:
来获取对框架的引用。
因此,现在当您为计时器创建 ActionListener 时,您可以将 Window 作为 ActionListener 的参数传递。
编辑:
mre 的建议简单直接,并且在许多情况下易于使用(在这种情况下可能是更好的解决方案)。
我的建议稍微复杂一些,但它向您介绍了 SwingUtilities 方法,该方法最终将允许您编写更多可重用的代码,这些代码可能被您可能创建的任何框架或对话框使用。
一个简单的例子如下:
当然,当窗口达到某个最小尺寸时,您会希望停止计时器。我将该代码留给您。
So when you click the button you have access to the button. Then you can use:
to get a reference to the frame.
So now when you create the ActionListener for the Timer you can pass in the Window as an argument for the ActionListener.
Edit:
The suggestion by mre is simple and straight forward and easy to use in many cases (and probably the better solution in this case).
My suggestion is a little more complicated but it was introducing you to the SwingUtilities method which will eventually allow you to write more reusable code that could potentially be used by any frame or dialog you might create.
A simple example would be something like:
Of course you would want to stop the timer when the winow reaches a certain minimum size. I'll leave that code up to you.