gpt4 book ai didi

java - 避免在 JEditorPane 中使用

中的填充/边距

转载 作者:行者123 更新时间:2023-12-02 04:51:50 25 4
gpt4 key购买 nike

我想在 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

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

最佳答案

看起来您可以使用<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>");

关于java - 避免在 JEditorPane 中使用 <p> 中的填充/边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29149227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com