如果我们想多次使用不同实现的 mouseClick 方法怎么办

What if we want to use mouseClick Method many times with different implementation

我们如何在不同的实现中使用相同的方法(相同的参数和 return 类型) 换句话说,在 java Gui 中,我想在一个 class 中以多种不同的方式使用 mouseClick 方法,这怎么可能?

您可以实现不同的 MouseListener 并将它们添加到每个您想要不同鼠标点击行为的组件中。

编辑:我在下面添加了一个示例。

public void example() {

    JPanel panel1 = new JPanel();
    panel1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            /* your code here, what should happen when the mouse clicked the panel */
        }
    });

    JTable table1 = new JTable();
    table1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            /* your code here, what should happen when the mouse clicked the table */
        }
    });
}

这里我使用 new MouseAdapter() more here 而不是 new MouseListener(),它实现了 MouseListener,因为这允许您只实现 MouseListener 接口的一个方法。