gpt4 book ai didi

java - 当鼠标悬停在 jlable 上时将显示图片的面板

转载 作者:行者123 更新时间:2023-12-01 12:42:29 26 4
gpt4 key购买 nike

我想让 jpanel1 不可见,直到将鼠标悬停在其中一个 jlabel 上。然后,JPanel1 必须变得可见,并且 JLable(称为 jLabel1)必须将其图像设置为鼠标悬停在其上的 JLabel 之一。

public GUI() 
{

initComponents();
jPanel1.setVisible(false);



for(int i =4; i<12;i++)
{
JLabel l = new JLabel();
l.setIcon(new ImageIcon(i+".jpg"));
p1.add(l);
}
for(int i =12; i<20;i++)
{
JLabel l = new JLabel();
l.setIcon(new ImageIcon(i+".jpg"));
p2.add(l);
}
}

下面是我的程序的两张图片的链接,一张在设计 View 中显示侧面板,另一张是运行时的实际程序。我希望当用户将鼠标悬停在图片网格中的 jlabel 之一上时,左侧的 JPanel 变得可见(请参见正在运行的程序的屏幕截图)。左侧面板顶部的 jLabel 必须将其图像设置为用户将鼠标悬停在图像网格中的图像。

enter image description here

enter image description here

最佳答案

我认为您已经在 J​​Label 网格中保存了缩略图 ImageIcons 网格,并且您希望在网格左侧显示较大的缩略图图像。如果是这样:

  • 我不会让 JPanel 出现和消失。
  • 而是在左侧放置一个包含 JLabel 的 JPanel。
  • 提供您的 JLabels MouseMotionListener。
  • 当鼠标进入标签时,让监听器获取相应的大 ImageIcon(与缩略图相对应的 ImageIcon),并让左侧的大 JLabel 通过 setIcon(...) 显示它。 .
<小时/>

编辑
您在评论中指出:

I see how this would work, although I do not know how to get the appropriate image icon because all of the JLabels on the right that currently have the pictures are named the same name(from my for loop). How would I get the individual JLabel information(in this case the ImageIcon of that JLabel.

请理解变量名称并不那么重要,重要的是对象引用。一种可能的解决方案是创建一个 map ,例如 HashMap<Icon, Icon> 。如果这样做,您可以将每个小缩略图 ImageIcon 与其较大的 ImageIcon 相关联。然后在你的MouseMotionListener中,通过 (JLabel) getSource() 获取鼠标输入的标签,通过 getIcon() 获取其缩略图图标,如果不为空(检查!),请使用您的 map 获取与缩略图关联的较大图标。

关于java - 当鼠标悬停在 jlable 上时将显示图片的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24974099/

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