谁能帮我写代码。我有两个图像 1.jpg
和 2.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);
}
});
}
}
最佳答案
您不应该在按钮内添加标签。只需将其图标设置为
button.setIcon(new ImageIcon(path1));
代替
button.add(pic1);
关于java - 图像翻转不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585960/