我有两个JButtons和一个JPanel。 如果用于注册鼠标侦听器的代码如下,我如何在JPanel上删除已注册的mouselistener。在单击一个JButton时,这个mouselistener被注册,现在点击其他JButton我想删除已注册的鼠标监听器。 如果我点击Second JButton点击这样的东西。
pnlImagePanel.removeMouseListener();
但是在上面的行中,removeMouseListener()方法需要监听器对象,而我没有实现鼠标监听器的类。我已经完成了一些注册监听器的编码,并且还自己编写了代码。
所以请指导我的朋友如何取消注册mouselistener。
以下是代码的一部分。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
Class abc{
pnlImagePanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
System.out.println("Mouse Pressed");
startingX = evt.getX();
startingY = evt.getY();
System.out.println(Startingx);
System.out.println(StartingY);
}
});
}
谢谢!
答案 0 :(得分:2)
创建一个实现MouseListener
类的类的对象,然后将其添加到MouseListener
进行侦听,而不是创建匿名JPanel
。删除时,将创建的MouseListener
对象传递给removeMouseListener()
函数。
答案 1 :(得分:2)
这是完整的代码
看看这是否适合你
package testapplications;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
/**
*
* @author sunil
*/
public class ButtonRegistrationTest extends javax.swing.JFrame
{
private javax.swing.JPanel panel;
private javax.swing.JButton register;
private javax.swing.JButton unregister;
/** Creates new form ButtonRegistration */
public ButtonRegistrationTest()
{
initComponents();
PanelMouseListener listener = new PanelMouseListener();
register.addActionListener(new RegisterMouseListener(listener));
unregister.addActionListener(new UnRegisterMouseListener(listener));
}
private class PanelMouseListener extends MouseAdapter
{
PanelMouseListener()
{
}
@Override
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(rootPane, "Clicked");
}
}
private class RegisterMouseListener implements ActionListener
{
PanelMouseListener listener;
RegisterMouseListener(PanelMouseListener listener)
{
this.listener = listener;
}
public void actionPerformed(ActionEvent event)
{
panel.addMouseListener(listener);
}
}
private class UnRegisterMouseListener implements ActionListener
{
PanelMouseListener listener;
public UnRegisterMouseListener(PanelMouseListener listener)
{
this.listener = listener;
}
public void actionPerformed(ActionEvent event)
{
panel.removeMouseListener(listener);
}
}
private void initComponents() {
panel = new javax.swing.JPanel();
register = new javax.swing.JButton();
unregister = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panel.setName("panel"); // NOI18N
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 196, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 173, Short.MAX_VALUE)
);
register.setText("Register");
register.setName("register"); // NOI18N
unregister.setText("Unregister");
unregister.setName("unregister"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(register, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(unregister, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(27, 27, 27))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(register)
.addGap(18, 18, 18)
.addComponent(unregister)))
.addContainerGap(127, Short.MAX_VALUE))
);
pack();
}
public static void main(String args[])
{
new ButtonRegistration().setVisible(true);
}
}
答案 2 :(得分:0)
您可以从下面的组件中删除所有mouseListeners。
MouseListener[] mouseListeners = component.getMouseListeners();
for (MouseListener mouseListener : mouseListeners) {
component.removeMouseListener(mouseListener);
}
希望这有帮助。