简体   繁体   English

如何在面板上使用addActionListener

[英]How can I use addActionListener on a panel

I have to classes that extends Panel and i want that one of the class to can use addActionListener and one to use addMouseListener. 我必须扩展面板的类,并且我希望该类之一可以使用addActionListener,而另一类使用addMouseListener。 I know that i have to override this methods but i tried and i really don't know how. 我知道我必须重写此方法,但我尝试过,但我真的不知道如何做。

public class QButton extends Panel implements MouseListener,ActionListener{
    public Label text;
    ImagePanel image;
    ActionListener listener;
    //.........

    public void addActionListener(ActionListener listener)
    {
        // What do i write here?`enter code here`
    }
}

public class ImagePanel extends Component {

    public Image resized;
    public String image;
    public MouseListener l;
    //...........
    public void paint(Graphics g) { 
    g.drawImage(resized, 0, 0, this);
    }

    public void addMouseListener(MouseListener l) {
        //What do i write here?`enter code here`
    }
}

you can use inbuilt moselistener same as actionlistener or for specific use your custom adapter Listner 您可以使用与动作侦听器相同的内置moselistener,也可以将自定义适配器侦听器用于特定用途

...//where initialization occurs: ... //发生初始化的位置:

 MyListener myListener = new MyListener();
 addMouseListener(myListener);
 addMouseMotionListener(myListener);

as specified at http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html中指定的

you can add MouseListener (or MouseAdapter) to any JComponents , but you can't add ActionListener to the JComponents that didn't implements this method, otherwise Compiler or IDE returns Exceptions 您可以将MouseListener (或MouseAdapter)添加到任何JComponents中 ,但是不能将ActionListener添加到未实现此方法的JComponents中,否则Compiler或IDE返回Exceptions

for example 例如

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Graphics2D");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents());//
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JPanel implements MouseListener, ActionListener {

    private static final long serialVersionUID = 1L;

     public CustomComponents() {
         addMouseListener(this);
         //addActionListener(this);//JPanel doesn't implements ActionListener directly
     }


    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }



    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }

    public void mouseClicked(MouseEvent e) {
        System.out.println("mouseClicked");
    }

    public void mousePressed(MouseEvent e) {
        System.out.println("mousePressed");
    }

    public void mouseReleased(MouseEvent e) {
        System.out.println("mouseReleased");
    }

    public void mouseEntered(MouseEvent e) {
        System.out.println("mouseEntered");
    }

    public void mouseExited(MouseEvent e) {
        System.out.println("mouseExited");
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("actionPerformed");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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