gpt4 book ai didi

java - 单击行时如何在鼠标附近显示图像

转载 作者:行者123 更新时间:2023-11-30 09:40:23 24 4
gpt4 key购买 nike

我的代码是这样的

public ImageIcon pictures[]=new ImageIcon[100];
jLabel10.setIcon(pictures[jTable1.getSelectedRow()]);
jLabel10.setLocation(getMousePosition().x,getMousePosition().y);

如何在点击事件中显示鼠标指针附近的图像(我是说在 jTable 中)?

最佳答案

对标签进行默认配置,假设您在 3x3 表格中有 9 个标签,这些标签有 9 个图像路径:

JLabel[][] labels = new JLabel[3][3];
String[][] paths = new String[3][3];

在您的 MouseListener 实现中,您可以将一些文本附加到单击的标签以显示图像:

table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int row = jTable.rowAtPoint(e.getPoint());
int col = jTable.columnAtPoint(e.getPoint());
// Assuming you have initialized the labels array and paths array.
labels[row][col].setText(labels[row][col].getText()
+ "<html><img src=\""
+ YourClass.class.getResource(paths[row][col])
+ "\">);
}
}

关于java - 单击行时如何在鼠标附近显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9445769/

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