0

我有一个 JComponent,我希望它在按下组件时执行一段代码。有人能帮我吗?

4

2 回答 2

0

假设您有一个扩展 JComponent 的类

import java.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyClass extends JComponent{
    public MyClass(){

        //Other code

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // Place your code here
            }
        });

        //Other code
    }
}
于 2012-08-02T16:14:27.653 回答
0
JComponent comp = new JPanel();
comp.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(MouseEvent e) {
        // Place your code here
    }
});
于 2012-08-02T15:06:28.337 回答