2012-02-25 60 views
2

我的代码看起来像如何显示鼠标附近的图像上的排

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

点击我怎样才能显示在点击事件(我的意思是在JTable)将鼠标指针附近的图像时?

回答

2

让你的标签的默认配置,让我们假设你有一个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]) 
           + "\">);   
    } 
} 
+0

我想在mouse.did附近显示此图像您注意到该问题? – 2012-02-25 17:44:39

+0

“靠近鼠标”你的意思是类似于工具提示吗?如果是这样的话,编辑你的问题,你需要一个类似工具提示的东西,当你将鼠标悬停在标签上时,会显示标签图片,而不是“点击”。好?那么我将以这种方式改变我的答案,为你的问题工作。 – Juvanis 2012-02-25 17:49:16

相关问题