避免在 JEditorPane 中使用 padding/margin in <p>
Avoid padding/margin in <p> used in JEditorPane
我想在 Jlabel 工具提示中有一个可点击的 link。由于不支持开箱即用,我找到了 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 一起使用时,顶部有一个空白,我不知道如何删除。我试过使用 margin 和 padding 但没用。
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);
}
});
}
}
结果如下所示:
问题:谁能告诉我如何避免工具提示顶部出现空隙?
您似乎可以使用 <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>");
我想在 Jlabel 工具提示中有一个可点击的 link。由于不支持开箱即用,我找到了 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 一起使用时,顶部有一个空白,我不知道如何删除。我试过使用 margin 和 padding 但没用。
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);
}
});
}
}
结果如下所示:
问题:谁能告诉我如何避免工具提示顶部出现空隙?
您似乎可以使用 <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>");