java - JFrame拖动监听器?
问题描述
我正在寻找某种 WindowMotionListener 或类似的东西,所以当我通过标题栏(或其他方式)拖动窗口时,我可以像使用 MouseMotionListener 一样获取位置信息。我正在尝试制作一个类似于 MFC 的 Swing 组件,您可以在其中将一个窗口拖到一个位置并让它卡入其中(例如:https ://imgur.com/LWSXv9x )。
到目前为止,当用户通过其内容拖动框架时,我已经完成了捕捉和拖动,但不是通过标题栏,这对最终用户来说会容易得多。有没有办法获得某种窗口拖动事件?我是否必须制作自己的窗户装饰并为它们添加“拖动”?
任何帮助将不胜感激
解决方案
我想在窗口移动时获取事件。...我的主要问题是无法确定用户是否正在拖动 jframe
那么,关于...ComponentListener
的JFrame
呢?
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Main {
private static void createAndShowGUI() {
final JFrame frame = new JFrame("Drag frame");
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentMoved(final ComponentEvent e) {
System.out.println("New frame location: " + frame.getLocation());
}
@Override
public void componentResized(final ComponentEvent e) {
System.out.println("New frame size: " + frame.getSize());
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JLabel("Drag the title bar and see the logs..."));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(Main::createAndShowGUI);
}
}
推荐阅读
- extjs - ExtJS 试图将记录从一个网格添加到另一个允许重复的网格
- r - 使用 InsertUI 和 UpdateSelectizeInput
- java - Java 是缓存整个对象还是仅缓存部分对象?(可见性问题)
- json - TMSL Xmla 在现有数据库上创建表 - 表格模型脚本语言 - Azure Analysis Services
- python - 仅当变量 ==1 时才执行 Python 定义
- amazon-web-services - 如何在 AWS SAM Cloud-Formation 中使用 Route53 设置自定义域名
- php - 使用pdf js打开时加载错误的文档
- javascript - 我正在使用 getElement 但它在我的代码中不起作用,为什么?
- java - JavaFX 错误:不知道如何检测 os 系列“primarystages”
- ios - 如何从 swift 数据中获取字节