如何在面板上使用addActionListener

时间:2011-12-05 10:55:20

标签: java swing awt

我必须扩展Panel的类,我希望其中一个类可以使用addActionListener,而另一个类可以使用addMouseListener。我知道我必须覆盖这些方法,但我尝试了,我真的不知道如何。

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`
    }
}

2 个答案:

答案 0 :(得分:2)

你可以使用与actionlistener相同的内置moselistener或特定用途你的自定义适配器Listner

... //初始化发生的地方:

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

http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

指定

答案 1 :(得分:1)

您可以将MouseListener(或MouseAdapter)添加到任何JComponents,但是您无法将ActionListener添加到没有实现的JComponents此方法,否则编译器或IDE将返回异常

例如

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