gpt4 book ai didi

Java 删除并稍后添加回 MouseListener

转载 作者:行者123 更新时间:2023-12-01 17:07:17 27 4
gpt4 key购买 nike

我有 4 JLabel s。

首次点击时:

我将背景颜色更改为红色并删除了 JLabelMouseListener我点击过的。

第二次点击时:

我将背景颜色更改为绿色,但是JLabel我之前单击的不应从红色变为绿色,因为我已经删除了 MouseListener .

第三次点击时:

我想添加回MouseListenerJLabel MouseListener第一次单击时已删除并将背景颜色更改为黑色,但我不知道如何添加回来。

我尝试使用addMouseListener我的方法addbackMouseListener(JLabel label)但似乎我无法在参数中传入“this”,并且我不知道 addMouseListener 的参数要传入什么.

public void addbackMouseListener(JLabel label) {
label.addMouseListener(this); <-- can't pass in this, what can I do?
}
<小时/>

代码:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


@SuppressWarnings("serial")
public class rMouseListener extends JFrame {
private JLabel[] jLabel = {new JLabel(), new JLabel(), new JLabel(), new JLabel()};;
private JPanel panel = new JPanel(new FlowLayout());
private int clickCount = 0;
private boolean mouseRemoved = false;

public rMouseListener() {

for(int i = 0; i< 4; i++) {
jLabel[i].setText(i + " ");
panel.add(jLabel[i]);
jLabel[i].addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JLabel label =(JLabel) e.getSource();
clickCount++;

/*remove the mouseListener for the label that is clicked*/
if(clickCount == 1) {
label.setBackground(Color.red);
label.setOpaque(true);
label.removeMouseListener(this);
mouseRemoved = true;
}

/* to verify that the mouseListener for that label is removed after
first click.*/
else if(clickCount == 2) {
label.setBackground(Color.green);
label.setOpaque(true);
}

/*check if the mouseListener is removed.
add back the mouseListener to the one that is perviously
removed at clickCount = 1 if it's removed*/
else if(clickCount == 3) {
if(mouseRemoved) {
addbackMouseListener(label);
label.setBackground(Color.black);
}
}


}
});
}
add(panel);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setSize(400,600);
setLocationRelativeTo(null);
setVisible(true);
}

public void addbackMouseListener(JLabel label) {
label.addMouseListener(this); <-- can't pass in this, what can I do?
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new rMouseListener();
}
});
}
}

最佳答案

this 引用 rMouseListener,它扩展了 JFrame,但它没有实现 MouseListener

您最好的选择可能是让您的原始 MouseAdapter 成为一个内部类,并在创建类时创建它的实例,然后只需添加和删除它即可

例如...

public class rMouseListener extends JFrame {
//...
private MouseListener mouseListener;

public rMouseListener() {

mouseListener = new MouseHandler();
for(int i = 0; i< 4; i++) {
jLabel[i].setText(i + " ");
panel.add(jLabel[i]);
jLabel[i].addMouseListener(mouseListener);
}
//...
}

public void addbackMouseListener(JLabel label) {
label.addMouseListener(mouseListener);
}

//...

public MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
JLabel label =(JLabel) e.getSource();
clickCount++;

/*remove the mouseListener for the label that is clicked*/
if(clickCount == 1) {
label.setBackground(Color.red);
label.setOpaque(true);
label.removeMouseListener(this);
mouseRemoved = true;
}

/* to verify that the mouseListener for that label is removed after
first click.*/
else if(clickCount == 2) {
label.setBackground(Color.green);
label.setOpaque(true);
}

/*check if the mouseListener is removed.
add back the mouseListener to the one that is perviously
removed at clickCount = 1 if it's removed*/
else if(clickCount == 3) {
if(mouseRemoved) {
addbackMouseListener(label);
label.setBackground(Color.black);
}
}


}
}
}

关于Java 删除并稍后添加回 MouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24950119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com