普通控件的鼠标监听事件
import javax.swing.*;import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class MyLayout extends JFrame{
private JPanel jp;
private JLabel jl1;
public static void main(String args[]){
MyLayout jf = new MyLayout();
jf.setVisible(true);
jf.setResizable(false);
}
MyLayout(){
setBounds(100,100,450,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new JPanel();
jl1 = new JLabel();
jl1.setIcon(new ImageIcon("./logo.png"));
setListener();
jp.add(jl1);
jp.setLayout(new FlowLayout());
setContentPane(jp);
}
void setListener(){
jl1.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
JFrame jf = new JFrame();
jf.setBounds(200,200,450,300);
jf.setVisible(true);
}
});
}
}
给图片添加一个鼠标双击事件非常简单,为了美观我单起了一个setListener函数,只要在addListener方法里重写一个监听器就行了,在重写的函数里我新建一个窗口,效果图如下:
页:
[1]