Java JPanel 标题边框,带有标题的工具提示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18692834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:14:07  来源:igfitidea点击:

JPanel titled border with tooltip text for the title

javaswingjpaneltooltipborder

提问by Hamid Fadishei

I am using setBorder(BorderFactory.createTitledBorder(title))in my JPanelin order to group its content in a rectangle with a title above it. How can I set a tooltip text for the title?

setBorder(BorderFactory.createTitledBorder(title))在 myJPanel中使用,以便将其内容分组在一个矩形中,上面有一个标题。如何为标题设置工具提示文本?

采纳答案by camickr

You can override the getToolTipText()method of the panel to check if the mouse of over the title text:

您可以覆盖getToolTipText()面板的方法来检查鼠标是否悬停在标题文本上:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitledBorderTest
{
    private static void createAndShowUI()
    {
        UIManager.getDefaults().put("TitledBorder.titleColor", Color.RED);
        Border lowerEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        TitledBorder title = BorderFactory.createTitledBorder(lowerEtched, "Title");
//      title.setTitleJustification(TitledBorder.RIGHT);
        Font titleFont = UIManager.getFont("TitledBorder.font");
        title.setTitleFont( titleFont.deriveFont(Font.ITALIC + Font.BOLD) );

        JPanel panel = new JPanel()
        {
            @Override
            public String getToolTipText(MouseEvent e)
            {
                Border border = getBorder();

                if (border instanceof TitledBorder)
                {
                    TitledBorder tb = (TitledBorder)border;
                    FontMetrics fm = getFontMetrics( tb.getTitleFont() );
                    int titleWidth = fm.stringWidth(tb.getTitle()) + 20;
                    Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());
                    return bounds.contains(e.getPoint()) ? super.getToolTipText() : null;
                }

                return super.getToolTipText(e);
            }
        };
        panel.setBorder( title );
        panel.setToolTipText("Title With ToolTip");

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

This code assumes the title is on the left. If you want the title on the right then you would need to adjust the Xvalue of the text bounds.

此代码假定标题在左侧。如果您想要右侧的标题,则需要调整X文本边界的值。

回答by Maxim Shoustin

I don't think you can add setToolTipTextto TitledBorder. you can provide tooltip for JComponentbut TitledBorderis not derived from JComponent.

我不认为你可以添加setToolTipTextTitledBorder. 您可以提供工具提示,JComponentTitledBorder不是从JComponent.

You can try to use JPanelinstead:

您可以尝试JPanel改用:

 ToolTipManager.sharedInstance().registerComponent(new JPanel());
 //ToolTipManager.sharedInstance().setDismissDelay(800000);

回答by kiheru

A possible approach is nesting components. As Borders are not components they can not have tooltips, but you can have a component with the sole purpose of holding border and the tooltip:

一种可能的方法是嵌套组件。由于边框不是组件,它们不能有工具提示,但您可以拥有一个仅用于保存边框和工具提示的组件:

JPanel outer = new JPanel();
outer.setBorder(BorderFactory.createTitledBorder("Title"));
outer.setToolTipText("sample text");
JPanel inner = new JPanel();
outer.add(inner);

and then use inneras the container for the components you want to group.

然后inner用作要分组的组件的容器。

回答by mKorbel

  • TollTip isn't right Components for experiments, all goog workaround for popup or tooltips are based on JWindow/ undecorated JDialog

  • maybe not necessary, keys in UIManager are accesible, but in this case all TollTips has the same settings

  • TollTip 不适合用于实验的组件,弹出窗口或工具提示的所有 goog 解决方法都基于 JWindow/未修饰的 JDialog

  • 也许不是必需的,UIManager 中的键是可以访问的,但在这种情况下,所有 TollTips 都具有相同的设置

enter image description here

在此处输入图片说明

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.*;

public class ColoredToolTipExample extends JFrame {

    private static final long serialVersionUID = 1L;

    public ColoredToolTipExample() {
        Border line, raisedbevel, loweredbevel, title, empty;
        line = BorderFactory.createLineBorder(Color.black);
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        title = BorderFactory.createTitledBorder("");
        empty = BorderFactory.createEmptyBorder(1, 1, 1, 1);
        Border compound;
        compound = BorderFactory.createCompoundBorder(empty, line);
        UIManager.put("ToolTip.foreground", new ColorUIResource(Color.red));
        UIManager.put("ToolTip.background", new ColorUIResource(Color.yellow));
        UIManager.put("ToolTip.font", new FontUIResource(new Font("Verdana", Font.PLAIN, 18)));
        UIManager.put("ToolTip.border", new BorderUIResource(compound));
        JButton button = new JButton("Hello, world");
        button.setToolTipText("<html> - myText <br> - myText <br> - myText <br>");
        getContentPane().add(button);
        JFrame frame = new JFrame("Colored ToolTip Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ColoredToolTipExample();
            }
        });
    }
}