我想问一下,JLabel has是否默认实现了mouseMotionListener。
我使用addAWTEventListener方法使用Toolkit.getDefaultToolkit()和MOUSE_MOTION_EVENT_MASK,以便运行时能够知道鼠标在哪个控件上。在jButton, jFrame, jTextfield上,一切都工作得很好,但JLabel、JPanel和其他方面却不行。
如果我将上述代码添加到JLabel中
lblNewLabel_1.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
}
});起作用了。
我必须在现在没有MouseMotionListener的所有控件中添加类似的东西
发布于 2015-02-10 18:34:02
public class something implements MouseMotionListener{
public something(){
button.addMouseMotionListener(this);
frame.addMouseMotionListener(this);
panel.addMouseMotionListener(this);
JTextField.addMouseMotionListener(this);
}
//MouseMotionListener methods
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}和是的,如果您希望从它们获得一个操作,则必须添加一个mouseOrMotion侦听器或操作侦听器。
当您想要将JLabel添加到窗口时,不要直接添加它,而是先将JLabel添加到JPanel中,然后再添加window.add(面板)。因为JLabel是轻量级组件和JFrame重量级,可能会给侦听器带来很多问题。
如果这还不够请告诉我..。
https://stackoverflow.com/questions/28438132
复制相似问题