工程下有个images文件夹,里面有两个图片文件,分别为start-on.gif和start.gif.start-on和start相比图片多了个黑色边框。程序代码如下:
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);
}
}
想达到的效果就是鼠标移动到JLabel上,JLabel的图片切换成strat-on,有了黑色边框,看起来比较友好。。。。但是运行程序却不行。。。。求指导
------解决方案--------------------
楼主你的方法的名称写错了
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"));
}
});