MouseListener似乎不适合我 [英] MouseListener doesn't appear to be working for me
问题描述
我需要在我的导师的序言中不要让我们使用IDE。我们使用TextPad。我想点击这个标签,然后从H变为T。目前,当我点击标签什么也没做。我忘了什么?
I need to preface this with my instructor doesn't let us use IDE's. We use TextPad. I want to click on this label and it then change from "H" to "T". Currently when I click the label does nothing. What am I forgetting?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab3Label extends JLabel implements MouseListener {
int count = 0;
boolean flag = true;
public Lab3Label (int i) {
setLayout(new BorderLayout());
count = i;
this.setText("H");
this.setFont(new Font("Serif", Font.PLAIN, 60));
this.setBorder(BorderFactory.createLineBorder(Color.black));
}
public void mouseReleased(MouseEvent e)
{
if(flag){
this.setText("H");
flag = false;
}
else{
this.setText("T");
flag = true;
}
}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
推荐答案
您的 JLabel
实现 MouseListener
,但您还需要告诉 JLabel
将事件发送给自己。在构造函数的末尾,你需要说
Your JLabel
implements MouseListener
, but you also need to tell the JLabel
to send events to itself. At the end of the constructor you'll need to say
addMouseListener(this);
如果你记得你可以将任何类变成一个 MouseListener
,你必须将你的监听器连接到 JLabel
。 JLabel
是其自己的监听器这一事实并不能免除您的责任。
This makes more sense if you remember that you can make any class into a MouseListener
, and you'd have to connect your listener to your JLabel
. The fact that the JLabel
is its own listener doesn't absolve you of this responsibility.
这篇关于MouseListener似乎不适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!