避免在JEditorPane </p>中使用的<p>中填充/边距

时间:2015-03-19 15:52:09

标签: java swing jeditorpane

我希望在Jlabel工具提示中有一个可点击的链接。由于不支持开箱即用,我找到了this solution,我已经根据自己的需要进行了调整(参见下面的代码)。 使用常规工具提示,我习惯使用parapraphs自动将长工具提示包装到给定宽度(例如<html><p width="300">Setting 'File' will show a single video which will be played with the first subtitle found for the specified languages.<br><br>Using 'Folder' will show a folder containing a list of videos with different subtitles.</p></html>)。

现在,当我使用带有HyperLinkToolTip的段落时,顶部有一个余量,我无法弄清楚如何删除。我尝试过使用边距和填充,但它没有用。

package test;

import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ToolTipUI;

public class HyperLinkToolTip extends JToolTip {
    private static final long serialVersionUID = -8107203112982951774L;

    private JEditorPane theEditorPane;

    public HyperLinkToolTip() {
        setLayout(new GridLayout());

        theEditorPane = new JEditorPane();
        theEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
        theEditorPane.setContentType("text/html");
        theEditorPane.setEditable(false);
        theEditorPane.setForeground(new ColorUIResource(255, 255, 255));
        theEditorPane.setBackground(new ColorUIResource(125, 184, 47));

        theEditorPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    if(Desktop.isDesktopSupported())
                    {
                      try {
                        Desktop.getDesktop().browse(new URI(e.getDescription()));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (URISyntaxException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    }
                }
            }
        });
        add(theEditorPane);
    }

    public void setTipText(String tipText) {
        theEditorPane.setText(tipText);
    }

    public void updateUI() {
        setUI(new ToolTipUI() { });
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame(HyperLinkToolTip.class.getName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        JButton btn = new JButton() {
            private static final long serialVersionUID = -2927951764552780686L;

            public JToolTip createToolTip() {
                JToolTip tip = new HyperLinkToolTip();
                tip.setComponent(this);
                return tip;
            }

            // Set tooltip location
            public Point getToolTipLocation(MouseEvent event) {
                return new Point(getWidth() / 2, getHeight() / 2);
            }
        };

        btn.setText("Tooltip Test");
        btn.setToolTipText("<html><p width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</p></html>");

        panel.add(btn);
        frame.setContentPane(panel);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setSize(400, 400);
                frame.setVisible(true);
            }
        });
    }
}

结果如下: HyperLinkToolTip example

问题:有谁能告诉我如何避免工具提示顶部的差距?

1 个答案:

答案 0 :(得分:2)

看起来您可以使用<div>...</div>代码而不是<p>...</p> tags

        btn.setToolTipText("<html><div width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</div></html>");