【问题标题】:How to show an image near the mouse when click on the row单击行时如何在鼠标附近显示图像
【发布时间】:2012-03-15 18:57:10
【问题描述】:

我的代码是这样的

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

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

【问题讨论】:

    标签: java image swing jtable


    【解决方案1】:

    对标签进行默认配置,假设您在 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])
                                    + "\">);        
        }
    }
    

    【讨论】:

    • 我想在鼠标附近显示这张图片。你注意到这个问题了吗?
    • “靠近鼠标”是指工具提示之类的东西吗?如果是这样,请编辑您的问题,即您想要一个类似工具提示的东西,当您将鼠标悬停在标签上时会显示标签图像,而不是“点击”。好的?那么我会以这种方式改变我的答案来解决你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2011-11-13
    • 2018-09-06
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多