在下面的代码中,单击标签应该使其隐藏,但该方法永远不会被调用!我在这里想念什么?
package com.hermaryopto.lib.output.message;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CDisplayMessage {
JLabel label;
public CDisplayMessage(Container parent, int top, int left, int width, int height, int fontSize){
label = new JLabel ();
label.setFont(new Font("Arial", Font.PLAIN, fontSize));
label.setOpaque(true);
label.setBackground(Color.ORANGE);
label.setCursor(new Cursor(Cursor.HAND_CURSOR));
label.setBounds(top, left, width, height);
int w = width;
int h = height;
label.setMinimumSize(new Dimension(w,h));
label.setPreferredSize(new Dimension(w,h));
label.setMaximumSize(new Dimension(w,h));
parent.add(label);
//--------- This mouse pressed never gets called why?
label.addMouseListener(new MouseAdapter(){
public void MousePressed(MouseEvent e){
label.setVisible(false);
}
});
}
public void message(String sMessage, final Color color){
label.setText(sMessage);
}
public void repaint(){
label.repaint();
}
public static void main( String [] args){
JFrame frame = new JFrame();
CDisplayMessage cp = new CDisplayMessage(frame, 10,10, 100,100,22);
cp.message("click here to hide this text", Color.red);
frame.setVisible(true);
frame.setSize(new Dimension(200,200));
}
}