从内容区域拖动 jframe 窗口很简单,但问题就在这里。 当我在框架上添加标签时,即使在标签上拖动仍然可以, 但是当我为标签添加 mouseclicked 事件时,即使适配器中没有实际处理任何代码,当我在标签上拖动时也不会发生拖动,但在窗口的其余区域上拖动则可以。
另一件事是我想让窗口的不透明度随着鼠标的移动而变化, 用 this.setOpacity(0.9f); 设置标签的 mouseclicked 事件后,标签也不会响应此事件..
谁知道如何解决这个问题?
private void formMousePressed(java.awt.event.MouseEvent evt) {
offsetX = evt.getX();
offsetY = evt.getY();
}
private void formMouseDragged(java.awt.event.MouseEvent evt) {
java.awt.Component c = (Component) evt.getSource();
c.setLocation(evt.getXOnScreen() - offsetX, evt.getYOnScreen()- offsetY);
}
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
// nothing actually done in the Label's mouse handler
}
private void formMouseEntered(java.awt.event.MouseEvent evt) {
this.setOpacity(0.9f);
}
private void formMouseExited(java.awt.event.MouseEvent evt) {
this.setOpacity(0.4f);
}
最佳答案
it's still ok even drag on the label, but when i add mouseclicked event for the label, even no codes actually proccessed in the adapter
正确,MouseEvent 仅传递到一个组件,因此标签会阻止该事件传递到窗口。
因此,您需要将 MouseListener 添加到窗口和标签中,以便拖动窗口。
查看Moving Windows 。它是一个监听器,允许您相对于其父级移动任何组件。因此,您可以创建一个 ComponentMover,然后注册窗口的根 Pane 以允许您拖动窗口。但您还需要注册标签。
关于java - 从内容区域拖动/移动 JFrame/JDialog 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643449/