【问题标题】:Jbutton setTooltip() as ImageIcon?Jbutton setTooltip()作为ImageIcon?
【发布时间】:2013-12-21 15:36:08
【问题描述】:

我环顾四周并查看了这里的各种主题。我发现最接近我正在尝试做的是这个:

Hovering over JButtons and displaying a message

基本上我正在尝试替换 button.setToolTipText("");带有一个 ImageIcon。我将其用作您将在 JFrame 中访问的下一页的预览,以向用户提供下一页的想法或快速概览。 (我已经弄清楚了图像,而不是工具提示)。

根据我在各个线程中看到的内容,这是我尝试使用的内容,但显然它不起作用,因此我问了这个问题。

我使用的代码:Log.setToolTip(new ImageIcon(getIconLog));

【问题讨论】:

    标签: java swing tooltip jbutton imageicon


    【解决方案1】:

    您可以使用MouseListenerJLabel 作为工具提示。像这样……

    public static void main(String[] args){
         final JFrame f = new JFrame();
         f.setSize(500, 400);
         f.setVisible(true);
         f.setLayout(null);
         f.setDefaultCloseOperation(3);
    
         final JButton b = new JButton("ToolTip");
         b.setBounds(100, 100, 70, 70);
         f.add(b);
    
         final JLabel toolTip = new JLabel();
         b.addMouseListener(new MouseListener() {
    
            @Override
            public void mouseReleased(MouseEvent e) {
    
            }
    
            @Override
            public void mousePressed(MouseEvent e) {
    
            }
    
            @Override
            public void mouseExited(MouseEvent e) {
                f.remove(toolTip);
                f.repaint();
            }
    
            @Override
            public void mouseEntered(MouseEvent e) {
                try {
                    toolTip.setIcon(new ImageIcon(ImageIO.read(new File("your image"))));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                toolTip.setBounds(b.getLocation().x+b.getWidth(), b.getLocation().y-b.getHeight(), 100, 50);
                f.add(toolTip);
                f.repaint();
            }
    
            @Override
            public void mouseClicked(MouseEvent e) {
    
            }
        });
    
    }
    

    【讨论】:

    • 很好的答案。不需要那样做(你回答的比我需要的多)。但我可以从中挑选一些片段,你回答了另一个我实际上被困在 ++ 上的问题。
    猜你喜欢
    • 2015-07-25
    • 2013-05-14
    • 2012-06-08
    • 2014-11-30
    • 1970-01-01
    • 2019-03-12
    • 2012-10-24
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多