简体   繁体   中英

Why MousePressed never gets called?

In the code below, clicking on the label is supposed to make it hidden, but the method never gets called! what am I missing here?

    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));

   }
}

You are neither overriding nor overloading the right method in your mouse adapter.

MousePressed does not exist in MouseAdapter, therefore is a custom method for your MouseAdapter implementation that never is called. mousePressed is the method you are expecting to call. In eclipse IDE you can use helpers (I think it is ctrl+space) to see all the methods you can override for your anonymous classes.

Replace:

label.addMouseListener(new MouseAdapter() {
  public void MousePressed(MouseEvent e) {
    label.setVisible(false);
  }
});

for

label.addMouseListener(new MouseAdapter() {
  @Override
  public void mousePressed(MouseEvent e) {
    label.setVisible(false);
  };
});

...........

UPDATE:

Now, if you really, really want to call MousePressed (since that's your question, silly me!)

You can use it as this:

// --------- This mouse pressed never gets called why?
label.addMouseListener(new MouseAdapter() {
  @Override
  public void mousePressed(MouseEvent e) {
    MousePressed(e)
  };

  public void MousePressed(MouseEvent e) {
    label.setVisible(false);
  }
});

OR

MouseAdapter myMouseAdapter = new MouseAdapter() {
      public void MousePressed(MouseEvent e) {
        label.setVisible(false);
      }
    };
label.addMouseListener(myMouseAdapter);
myMouseAdapter.MousePressed(mouseEventInstance);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM