图像翻转无法正常工作

时间:2011-07-05 16:37:39

标签: java image swing

请有人帮我解决这个问题。我有两张图片1.jpg2.jpg,当我运行程序时,按钮上会显示1.jpg,但当我将鼠标悬停在按钮上时,2.jpg不会出现。以下是代码,谢谢

import javax.swing.*;

class ButtonRollover {
public static void main(String[] args) throws Exception {

    String path1 = ("C:\\1.jpg");
    String path2 = ("C:\\2.jpg");

    final JLabel pic1 = new JLabel(new ImageIcon(path1));
    final JLabel pic2 = new JLabel(new ImageIcon(path2));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {


             JButton button = new JButton("Hover");
             button.setRolloverIcon(new ImageIcon("C:\\2.jpg"));
             button.add(pic1);
             button.setRolloverEnabled(true);

             JOptionPane.showMessageDialog(null, button);
        }
    });
}
}

1 个答案:

答案 0 :(得分:2)

您不应该在按钮内添加标签。只需使用

设置其图标即可
button.setIcon(new ImageIcon(path1));

而不是

button.add(pic1);
相关问题