日期:2014-05-20 浏览次数:20744 次
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JLabelListener {
public static void main(String[] args) {
new JLabelListener().init();
}
JLabel startLabel=new JLabel(new ImageIcon("images/start.gif"));
JButton button=new JButton("按钮");
public void init(){
JFrame jf=new JFrame("监听");
jf.setLayout(new FlowLayout());
startLabel.addMouseListener(new MouseAdapter(){
public void MouseEntered(MouseEvent e){
startLabel.setIcon(new ImageIcon("images/start-on.gif"));
}
public void MouseExited(MouseEvent e){
startLabel.setIcon(new ImageIcon("images/start.gif"));
}
});
jf.add(startLabel);
jf.add(button);
jf.pack();
jf.setVisible(true);
}
}
startLabel.addMouseListener(new MouseAdapter(){那个mouseEntered的m的小写的
@Override
public void mouseEntered(MouseEvent e) {
startLabel.setIcon(new ImageIcon("images/start-on.jpg"));
}
@Override
public void mouseExited(MouseEvent e){
startLabel.setIcon(new ImageIcon("images/start.jpg"));
}
});