gpt4 book ai didi

java - 图像翻转不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 03:28:39 26 4
gpt4 key购买 nike

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

最佳答案

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

button.setIcon(new ImageIcon(path1));

代替

button.add(pic1);

关于java - 图像翻转不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com