实时监听JFrame高度和宽度
我遇到了一种情况,当用户拖动鼠标调整框架大小时,我希望能够知道 JFrame 的高度和宽度。
我对此进行了研究,以下问题提供的答案对我有一点帮助,但并非全部: 当用户拖动鼠标时监听 JFrame 调整大小事件?
我的问题是:我已经让我的“HSFrame”类扩展 JFrame 并实现ActionListener、MouseMotionListener(但是,ActionListener 与这个问题无关)。这个问题涉及的是MouseMotionListener。
我有一个名为“CanvasPanel”的单独类,它扩展了 JPanel——这是我使用图形对象通过绘制字符串来更新实时信息的地方。
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
validate();
canvas.repaint();
}
这是我的“HSFrame”类的代码摘录(扩展 JFrame,实现 MouseMotionListener)。在此代码示例中,“canvas”是一个 CanvasPanel 对象,每当调用 MouseDragged 时我都会调用它的 repaint() 方法。
我选择使用 MouseMotionListener,因为这(据说)会给我实时更新,而不是 ComponentListener 的 componentResized() 方法(与 MouseReleased 绑定)。
当我从左上角调整框架大小时,一切都会 100% 完美更新。高度、宽度、X 和 Y 值都完美地重新绘制。
但当我从右下角调整大小时,它不会实时更新。我不知道你怎么想,但我更喜欢从右下角调整东西的大小。
在“CanvasPanel”类本身中,我扩展了 ComponentListener 并向其中的“HSFrame”对象添加了一个侦听器 - 它不会实时更新,这很好,但它会在释放鼠标后重新绘制高度和宽度,无论 HSFrame 的大小是用哪个角调整的。
主要问题: 是否可以让 MouseMostionListener 知道我正在从右下角调整 JFrame 的大小?当我通过单击并拖动标题栏来移动框架时,它会听到声音,当我从左上角调整大小时(它也与标题栏交互),它会听到声音。它只是听不到任何内容发生在 JFrame 的其他边框上。
I've got a situation where I would like to be able to know a JFrame's height and width as the user drags their mouse to resize the frame.
I researched this, and the answers provided at the following question helped me a tiny bit, but not all the way: Listen to JFrame resize events as the user drags their mouse?
My problem is: I have made my "HSFrame" class extend JFrame and implement ActionListener, MouseMotionListener (ActionListener is irrelevant to this question, however). What this question pertains to is MouseMotionListener.
I have a separate class called "CanvasPanel" that extends JPanel--this is where I use a graphics object to update real-time information by drawing strings.
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
validate();
canvas.repaint();
}
This is a code excerpt from my "HSFrame" class (extends JFrame, implements MouseMotionListener). In this code example, "canvas" is a CanvasPanel object, and I am calling its repaint() method whenever MouseDragged is called.
I chose to use a MouseMotionListener because this (supposedly) would give me real-time updates as opposed to ComponentListener's componentResized() method (which is tied to MouseReleased).
Everything updates 100% perfectly when I resize the frame from the top-left corner. The height, width, X, and Y values all repaint perfectly.
But it does not update in real-time when I resize from the bottom-right corner. And I don't know about you, but I prefer to resize things from the bottom-right.
In the "CanvasPanel" class itself, I extended ComponentListener and added a listener to an "HSFrame" object there--it does not update in real-time, which was fine, but it does repaint the height and width after the mouse is released, regardless of which corner the HSFrame was resized with.
Main question:
Is it possible to get the MouseMostionListener to know that I am resizing the JFrame from the bottom-right? It hears when I move the frame around by clicking and dragging the title bar, and it hears when I resize from the top-left corner (which is also interacting with the title bar). It just doesn't hear when anything goes on to the other borders of the JFrame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必使用鼠标来执行此操作!用简单的方法来做:
编写一个组件监听器。让 HSFrame 实现
ComponentListener
并将调整大小代码放入
public void componentResized(ComponentEvent e)
中。我认为 mouseListener 不起作用,因为当您从右下角拖动时,鼠标不在
JFrame
中,因此事件侦听器不会拾取它。You don't have to do this with the mouse! Do it the easy way:
Write a component listener. Have HSFrame implement
ComponentListener
and put your resize code inpublic void componentResized(ComponentEvent e)
.I think that the mouseListener won't work because when you're dragging from the bottom right your mouse is not in the
JFrame
, so the event listener doesn't pick it up.