【问题标题】:Image Rollover not working图像翻转不起作用
【发布时间】:2011-09-28 23:33:24
【问题描述】:

谁能帮我写代码。我有两个图像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);
        }
    });
}
}

【问题讨论】:

    标签: java image swing


    【解决方案1】:

    您不应该在按钮内添加标签。只需将其图标设置为

    button.setIcon(new ImageIcon(path1));
    

    而不是

    button.add(pic1);
    

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2010-11-27
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多