6

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

4

4 回答 4

6

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

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

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

于 2013-09-09T07:27:55.657 回答
5

您可以覆盖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();
            }
        });
    }
}

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

于 2013-09-09T16:08:49.603 回答
1

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

您可以尝试JPanel改用:

 ToolTipManager.sharedInstance().registerComponent(new JPanel());
 //ToolTipManager.sharedInstance().setDismissDelay(800000);
于 2013-09-09T06:52:26.137 回答
1
  • TollTip 不是正确的实验组件,所有用于弹出或工具提示的 goog 解决方法都基于 JWindow/未修饰的 JDialog

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

在此处输入图像描述

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();
            }
        });
    }
}
于 2013-09-09T07:41:37.510 回答