Java GUI - MouseListener和ActionListener可以在同一个类中吗?

Mus*_*afa 4 java user-interface swing actionlistener mouselistener

CircleListener是我的面板类中的内部类,它现在涉及MouseListener接口.MouseRelased方法检查单击的区域是否被圆圈包围,如果是,则将该形状设置为选中并删除选定的形状.

现在我需要一个ActionListener来向这个面板添加一个带有"timer"对象的随机大小的圆圈.问题:是否可以将"ActionListener"实现到CircleListener,或者最好为"ActionListener"创建另一个内部类?

提前致谢

private class CircleListener implements MouseListener
{
    ShapesCanvas canvas;
    ShapeContainer container;
    Shape possibleShape;

    private CircleListener(ShapesCanvas canv, ShapeContainer cont)
    {
        this.canvas = canv;
        this.container = cont;
    }

    public void MouseRelased (MouseEvent e)
    {
        possibleShape = container.contains( e.getX(), e.getY());

        if( possibleShape != null)
        {
            ( (Selectable)possibleShape).setSelected(true);
            container.removeSelected();
        }
        canvas.repaint(); //repaints the last situation
    }
Run Code Online (Sandbox Code Playgroud)

Jak*_*rka 11

这是绝对可能的,只是声明

private class CircleListener implements MouseListener, ActionListener
Run Code Online (Sandbox Code Playgroud)

你可以创建两个类,这实际上是我喜欢的,因为那时你有两个明确定义的不同实体.每个实体只负责一个功能.

但这两种方法都是有效的.