简体   繁体   English

Java(Swing)-一次拖动两个窗口

[英]Java (Swing) - Drag two windows at once

How can I have one window move with another? 如何使一个窗口移动到另一个窗口? ie, I'd like a JDialog to follow a JFrame when the JFrame is being dragged. 即,我希望JDialog在拖动JFrame时跟随JFrame。 If the JFrame moves by (+5, +20), the JDialog needs to move the same. 如果JFrame移动(+5,+20),则JDialog需要移动相同的对象。

I've tried using ComponentListeners, but I only receive drag events in chunks which causes the JDialog window to be jumpy while dragging the main JFrame. 我已经尝试过使用ComponentListeners,但是我只收到大块的拖动事件,这会导致在拖动主JFrame时JDialog窗口跳动。 I've tried using MouseListeners, but I can't figure out how to detect events on the actual frame of the JFrame. 我曾尝试使用MouseListeners,但无法弄清楚如何在JFrame的实际框架上检测事件。

尝试使用ComponentListener.componentMoved事件,而不是监视JFrame上的拖动事件。

AFAIK here is no move multiple windows in AWT. AFAIK这里没有在AWT中移动多个窗口。 To get the moves to be called at a similar time, I guess you want the JFrame decorations to be PL&F rendered. 为了使这些动作在相似的时间被调用,我想您希望对JFrame装饰进行PL&F渲染。 Put in a PL&F-specific hack to do the moves yourself, moving both windows at almost the same time. 放入特定于PL&F的技巧,自己动手,几乎同时移动两个窗口。 You may still have a problem with exposing bits of windows only to cover them up causing some performance degradation. 您可能会遇到一些问题,就是只暴露一些窗口以掩盖它们,从而导致性能下降。

The component listener method works perfectly. 组件侦听器方法可以完美工作。 I did something like this: 我做了这样的事情:

Point p = this.getLocation();
p.x += this.getWidth() + 10;
this.getOwner().setLocation(p);

Where the '10' is the space between the current window(a JDialog) and its owner which is to its right. 其中“ 10”是当前窗口(一个JDialog)与其右侧之间的所有者之间的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM