下面列出了javax.swing.JTextPane#setHighlighter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Constructs an antialias-capable JTextPane with a DefaultHighlighter
* associated with it.
*
* @param attributes - see {@link edu.mit.csail.sdg.alloy4.OurUtil#make
* OurUtil.make(component, attributes...)}
*/
public static JTextPane pane(Function<MouseEvent,String> tooltip, Object... attributes) {
JTextPane ans = new JTextPane() {
static final long serialVersionUID = 0;
@Override
public void paint(Graphics gr) {
if (antiAlias && gr instanceof Graphics2D) {
((Graphics2D) gr).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
super.paint(gr);
}
@Override
public String getToolTipText(MouseEvent event) {
if (tooltip != null) {
return tooltip.apply(event);
}
return super.getToolTipText(event);
}
};
if (tooltip != null)
ToolTipManager.sharedInstance().registerComponent(ans);
OurUtil.make(ans, attributes);
ans.setHighlighter(new DefaultHighlighter());
map.put(ans, Boolean.TRUE);
return ans;
}
public EditorCurrentLineHighlighter(JTextPane textPane, Color highlightColor) {
Color c = highlightColor != null ? highlightColor : DEFAULT_COLOR;
MyHighlighter h = new MyHighlighter();
textPane.setHighlighter(h);
painter = new DefaultHighlighter.DefaultHighlightPainter(c);
}
/**
* Constructs a {@link ClipboardFrame}.
*
* @param content
* the content as a string
*/
public ClipboardFrame(final String content) {
final JTextPane text = new JTextPane() {
private static final long serialVersionUID = 1L;
@Override
public void processMouseEvent(MouseEvent me) {
switch (me.getID()) {
case MouseEvent.MOUSE_CLICKED:
switch (me.getButton()) {
case MouseEvent.BUTTON1:
StringSelection stringSelection = new StringSelection(content);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, ClipboardFrame.this);
break;
default:
break;
}
ClipboardFrame.this.dispose();
break;
default:
break;
}
}
};
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
ClipboardFrame.this.dispose();
}
});
text.setCaretColor(Color.WHITE);
text.setEditable(false);
text.setHighlighter(null);
text.setBorder(BorderFactory.createLineBorder(Color.WHITE, 4));
final StyledDocument doc = text.getStyledDocument();
try {
doc.insertString(doc.getLength(), content, null);
doc.remove(doc.getLength() - 1, 1);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
JPanel panel = new JPanel(new BorderLayout());
panel.add(text);
panel.add(BorderLayout.SOUTH, new JLabel(" left-click: copy to clipboard"));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
add(panel);
setUndecorated(true);
Point2D p = MouseInfo.getPointerInfo().getLocation();
setLocation((int) p.getX() - 8, (int) p.getY() - 8);
}