我有这三个图像应该在点击时循环,而当我点击时我得到gif图像,其他两个图像不响应,它们仍然在鼠标滚动时改变。 我已将setRolloverEnabled方法设置为false以阻止我的图像在鼠标滚动时更改,但它们会继续更改。我无法发现我的错误,有人可以指出我的错误。
public class ImageButton extends JFrame {
private final JButton imgButton;
private final Icon clickImage;
public ImageButton() {
super("Lab button");
Icon image1 = new ImageIcon(getClass().getResource("/imageBtn/Images/Image1.png"));
Icon image2 = new ImageIcon(getClass().getResource("/imageBtn/Images/Image2.png"));
clickImage = new ImageIcon(getClass().getResource("/imageBtn/Images/Image3.gif"));
imgButton = new JButton(image1);
imgButton.setRolloverIcon(image2);
add(imgButton);
ButtonClickEventHandler buttonEventHandler = new ButtonClickEventHandler();
imgButton.addActionListener(buttonEventHandler);
}
private class ButtonClickEventHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
imgButton.setRolloverEnabled(false);
imgButton.setIcon(clickImage);
}
}
导入javax.swing.JFrame;
public class ImageButtonApp {
public static void main(String[] args) {
ImageButton image = new ImageButton();
image.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
image.setSize(660, 660);
image.setVisible(true);
}
}