当鼠标输入按钮时,jlabel幻灯片
我无法工作:
public void ru() throws InterruptedException {
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/room.jpg")));
setVisible(true);
Thread.sleep(400);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/ROOMS.jpg")));
setVisible(true);
Thread.sleep(400);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/background.jpg")));
setVisible(true);
}
private void BtnRoomsMouseEntered(java.awt.event.MouseEvent evt) {
try {
ru();
} catch (InterruptedException ex) {
}
}
有什么解决方案吗? 当鼠标输入特定按钮时,只要鼠标仍在按钮中,我希望Jlabel显示出幻灯片显示。如果鼠标碰巧退出按钮,则jlabel将返回到空状态。 (不再有任何东西。)可能吗?请帮忙。我还尝试使用语句使用,但没有好处。顺便说一句,我正在使用Netbeans。
I can't get this to work:
public void ru() throws InterruptedException {
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/room.jpg")));
setVisible(true);
Thread.sleep(400);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/ROOMS.jpg")));
setVisible(true);
Thread.sleep(400);
jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/adv/background.jpg")));
setVisible(true);
}
private void BtnRoomsMouseEntered(java.awt.event.MouseEvent evt) {
try {
ru();
} catch (InterruptedException ex) {
}
}
Any solutions?
I want a jLabel to show a slide show when a mouse enters a certain button and stays like that as long as the mouse is still in a button. If the mouse happens to exit the button, the jlabel will return to a null state. (no more anything.) Is that possible? Please help. I also tried using for
statement but no good. I'm using netbeans by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要实现
javax.swing.timer
。尝试调用thread.sleep()
将阻止事件调度线程。参见如何使用摇摆计时器。这是delay
是您想要延迟的时间,是侦听器
将收听Timer
action> actionevent 触发了每个
延迟
毫秒。因此,您需要这样的
按钮,您需要使用
鼠标录制
和mouseexited
,然后您只需调用timer.start。 ()
或timer.stop()
如果您不知道如何添加
mouselistener
右键单击“设计视图”的按钮,然后选择<代码>事件 - &GT;鼠标 - &gt;鼠标的。对于Mouseexited
做同样的事情。您应该看到上述为您自动生成的方法。更新
First thing you need to implement a
javax.swing.Timer
. Trying to callThread.sleep()
will block the Event Dispatch Thread. See How to use Swing Timers. Here is the basic constructwhere
delay
is the time in milliseconds you want delayed and thelistener
will listen for theTimer
ActionEvent
fired everydelay
milliseconds.So you want something like this
For your button, you need to use
mouseEntered
andmouseExited
, then you can just calltimer.start()
ortimer.stop()
If you don't know how to add the
MouseListener
just right click on the button from the design view and selectEvent -> Mouse -> mouseEntered
. Do the same formouseExited
. You should see the above methods auto-generated for you.UPDATE
您需要将
mouseadapter
添加到jlabel
并使用其mouseTentered()
和mouseexited()
,如下:You want to add a
MouseAdapter
to theJLabel
and use itsmouseEntered()
andmouseExited()
as follows: