java窗口事件,Java中的窗口拖动事件

I'm trying to implement an OS X drawer like feature in Java, and so I'm going to have one window hiding under another. However when I drag the primary window (JFrame) I need to send updates as it moves to the secondary window (JWindow) below.

So it'd be something like this…

---------------

| |----------

| JFrame | |

| | JWidow |

| | |

| | |

| | |

| |-----------

---------------

That said, ComponentListener in OS X doesn't send constant updates for componentMoved, only when you pause for a second or lift off the mouse. This seems to differ from Win/Linux, so I was wondering if anyone had a different solution.

Without completely understanding everything, I was hoping to get and deal with whatever is drawing the window's titlebar (something about the rootpane it seems). I'm not even sure if that is going to be possible to do, but it is the only other solution I can think of to determine when the whole window is being dragged.

Any help is appreciated!

解决方案

Could you use a mouseMotionListener to see if the mouse is moving, and if it is, update the position of the second window using getLocation(). You can also check that the current position is equal to the previous position to prevent wasting resources. I'm not sure if getLocation() will work properly though, it really does depend on how the Operating System handles it.

javascrtpt 实现窗口拖拽、移动功能、显示坐标代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html <head runat="server"> <title></title> [removed] var mouseX, mouseY; var objX, objY; var isDowm = false; //是否按下鼠标 function mouseDown(obj, e) { obj.style.cursor = "move"; objX = div1.style.left; objY = div1.style.top; mouseX = e.clientX; mouseY = e.clientY; isDowm = true; } function mouseMove(e) { var div = document.getElementById("div1"); var x = e.clientX; var y = e.clientY; if (isDowm) { div.style.left = parseInt(objX) + parseInt(x) - parseInt(mouseX) + "px"; div.style.top = parseInt(objY) + parseInt(y) - parseInt(mouseY) + "px"; document.getElementById("span1")[removed] = "x:" + div.style.top + " " + "y:" + div.style.left; } } function mouseUp(e) { if (isDowm) { var x = e.clientX; var y = e.clientY; var div = document.getElementById("div1"); div.style.left = (parseInt(x) - parseInt(mouseX) + parseInt(objX)) + "px"; div.style.top = (parseInt(y) - parseInt(mouseY) + parseInt(objY)) + "px"; document.getElementById("span2")[removed] = "x:" + div.style.top + " " + "y:" + div.style.left; mouseX = x; rewmouseY = y; div1.style.cursor = "default"; isDowm = false; } } [removed] </head> <body> <span id="span1"></span></br><span id="span2"></span></br> <div id="div1" Green; border: 1px solid red; height: 300px; top: 100px; left: 100px; width: 300px; position: absolute;" </div> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值