下面列出了javax.swing.JTextPane#putClientProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private JComponent getElementsComponent (String msg) {
JTextPane area = new JTextPane ();
area.setEditable (false);
area.setContentType ("text/html"); // NOI18N
area.setText (msg);
area.setOpaque (false);
area.setBackground(new Color(0, 0, 0, 0));
area.putClientProperty( JTextPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE );
return area;
}
private List<Highlight> getHighlights(JTextPane pane) {
List<Highlight> highlights = (List<Highlight>) pane.getClientProperty(HIGHLIGHTS_PROPERTY);
if(highlights == null) {
highlights = new LinkedList<Highlight>();
pane.putClientProperty(HIGHLIGHTS_PROPERTY, highlights);
}
return highlights;
}
public JAboutFrame(JByteMod jbm) {
this.setTitle(JByteMod.res.getResource("about") + " " + jbm.getTitle());
this.setModal(true);
setBounds(100, 100, 400, 300);
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.setBorder(new EmptyBorder(10, 10, 10, 10));
setResizable(false);
JButton close = new JButton(JByteMod.res.getResource("close"));
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JAboutFrame.this.dispose();
}
});
JPanel jp = new JPanel(new GridLayout(1, 4));
for (int i = 0; i < 3; i++)
jp.add(new JPanel());
jp.add(close);
cp.add(jp, BorderLayout.PAGE_END);
JTextPane title = new JTextPane();
title.setContentType("text/html");
title.setText(TextUtils.toHtml(jbm.getTitle()
+ "<br/>Copyright \u00A9 2016-2018 noverify<br/><font color=\"#0000EE\"><u>https://github.com/GraxCode/JByteMod-Beta</u></font><br/>Donate LTC: <font color=\"#333333\">LhwXLVASzb6t4vHSssA9FQwq2X5gAg8EKX</font>"));
UIDefaults defaults = new UIDefaults();
defaults.put("TextPane[Enabled].backgroundPainter", this.getBackground());
title.putClientProperty("Nimbus.Overrides", defaults);
title.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
title.setBackground(null);
title.setEditable(false);
title.setBorder(null);
cp.add(title, BorderLayout.CENTER);
getContentPane().add(cp);
}
public ResourcePanel(Dimension max, Dimension min)
{
super("Resource Panel",false,false,false,false);
this.setMaximumSize(max);
this.setPreferredSize(min);
desc = new JTextPane();
desc.setEditable(false);
UIDefaults defaults = new UIDefaults();
defaults.put("TextPane[Enabled].backgroundPainter", AMLColor.WHITE);
desc.putClientProperty("Nimbus.Overrides", defaults);
desc.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
desc.setBackground(AMLColor.WHITE);
doc = desc.getStyledDocument();
def = StyleContext.getDefaultStyleContext(). getStyle(StyleContext.DEFAULT_STYLE);
bold = doc.addStyle("bold", def);
StyleConstants.setBold(bold, true);
s = doc.addStyle("source", def);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s, AMLColor.BLUE);
t = doc.addStyle("target", def);
StyleConstants.setBold(t, true);
StyleConstants.setForeground(t, AMLColor.BROWN);
u = doc.addStyle("uri", def);
StyleConstants.setUnderline(u, true);
setContentPane(desc);
pack();
setVisible(true);
refresh();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(MainWin.icon);
setSize(500, 300);
setResizable(false);
setLocationRelativeTo(this.getParent());
setTitle("About " + AppConfig.appTitle);
JPanel mainPanel = new JPanel(new BorderLayout());
add(mainPanel);
JPanel leftPanel = new JPanel();
mainPanel.add(leftPanel, BorderLayout.WEST);
ImageIcon iconImg = new ImageIcon(MainWin.icon);
iconImg.setImage(iconImg.getImage().getScaledInstance((int) (iconImg.getIconWidth() * 0.5), (int) (iconImg.getIconHeight() * 0.5), Image.SCALE_SMOOTH));
JLabel iconLabel = new JLabel(iconImg);
iconLabel.setBorder(BorderFactory.createEmptyBorder(6, 10, 6, 10));
leftPanel.add(iconLabel);
HyperlinkListener hlLsnr = new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
return;
// 超链接标记中必须带有协议指定,e.getURL()才能得到,否则只能用e.getDescription()得到href的内容。
// JOptionPane.showMessageDialog(InfoDialog.this, "URL:"+e.getURL()+"\nDesc:"+ e.getDescription());
URL linkUrl = e.getURL();
if (linkUrl != null) {
try {
Desktop.getDesktop().browse(linkUrl.toURI());
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "无法打开超链接:" + linkUrl + "\n详情:" + e1, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "超链接信息不完整:" + e.getDescription() + "\n请确保链接带有协议信息,如http://,mailto:", JOptionPane.ERROR_MESSAGE);
}
}
};
JTextPane infoArea = new JTextPane();
//设置css单位(px/pt)和chrome一致
infoArea.putClientProperty(JTextPane.W3C_LENGTH_UNITS, true);
infoArea.addHyperlinkListener(hlLsnr);
infoArea.setContentType("text/html");
infoArea.setText(getInfo());
infoArea.setEditable(false);
infoArea.setBorder(BorderFactory.createEmptyBorder(2, 10, 6, 10));
infoArea.setFocusable(false);
JScrollPane infoAreaScrollPane = new JScrollPane(infoArea);
infoAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
infoAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
infoAreaScrollPane.getViewport().setBackground(Color.WHITE);
mainPanel.add(infoAreaScrollPane, BorderLayout.CENTER);
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setIconImage(MainWin.icon);
setSize(500, 300);
setResizable(false);
setLocationRelativeTo(this.getParent());
setTitle("About " + AppConfig.appTitle);
JPanel mainPanel = new JPanel(new BorderLayout());
add(mainPanel);
JPanel leftPanel = new JPanel();
mainPanel.add(leftPanel, BorderLayout.WEST);
ImageIcon iconImg = new ImageIcon(MainWin.icon);
iconImg.setImage(iconImg.getImage().getScaledInstance((int) (iconImg.getIconWidth() * 0.5), (int) (iconImg.getIconHeight() * 0.5), Image.SCALE_SMOOTH));
JLabel iconLabel = new JLabel(iconImg);
iconLabel.setBorder(BorderFactory.createEmptyBorder(6, 10, 6, 10));
leftPanel.add(iconLabel);
HyperlinkListener hlLsnr = new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED)
return;
// 超链接标记中必须带有协议指定,e.getURL()才能得到,否则只能用e.getDescription()得到href的内容。
// JOptionPane.showMessageDialog(InfoDialog.this, "URL:"+e.getURL()+"\nDesc:"+ e.getDescription());
URL linkUrl = e.getURL();
if (linkUrl != null) {
try {
Desktop.getDesktop().browse(linkUrl.toURI());
} catch (Exception e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "无法打开超链接:" + linkUrl + "\n详情:" + e1, JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(AboutDialog.this, "超链接错误", "超链接信息不完整:" + e.getDescription() + "\n请确保链接带有协议信息,如http://,mailto:", JOptionPane.ERROR_MESSAGE);
}
}
};
JTextPane infoArea = new JTextPane();
//设置css单位(px/pt)和chrome一致
infoArea.putClientProperty(JTextPane.W3C_LENGTH_UNITS, true);
infoArea.addHyperlinkListener(hlLsnr);
infoArea.setContentType("text/html");
infoArea.setText(getInfo());
infoArea.setEditable(false);
infoArea.setBorder(BorderFactory.createEmptyBorder(2, 10, 6, 10));
infoArea.setFocusable(false);
JScrollPane infoAreaScrollPane = new JScrollPane(infoArea);
infoAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
infoAreaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
infoAreaScrollPane.getViewport().setBackground(Color.WHITE);
mainPanel.add(infoAreaScrollPane, BorderLayout.CENTER);
}
@Override
public void mouseMoved(MouseEvent e) {
JTextPane pane = (JTextPane)e.getSource();
StyledDocument doc = pane.getStyledDocument();
int offset = pane.viewToModel(e.getPoint());
Element elem = doc.getCharacterElement(offset);
Highlight h = getHighlight(pane, offset);
Highlight prevHighlight = (Highlight) pane.getClientProperty(PREV_HIGHLIGHT_PROPERTY);
AttributeSet prevAs = (AttributeSet) pane.getClientProperty(PREV_HIGHLIGHT_ATTRIBUTES);
// if(h != null && h.equals(prevHighlight)) {
// return; // nothing to do
// } else
if(prevHighlight != null && prevAs != null) {
doc.setCharacterAttributes(prevHighlight.startOffset, prevHighlight.endOffset - prevHighlight.startOffset, prevAs, true);
pane.putClientProperty(PREV_HIGHLIGHT_PROPERTY, null);
pane.putClientProperty(PREV_HIGHLIGHT_ATTRIBUTES, null);
}
int modifiers = e.getModifiers() | e.getModifiersEx();
if ( (modifiers & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK ||
(modifiers & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK)
{
AttributeSet as = elem.getAttributes();
if (StyleConstants.isUnderline(as)) {
// do not underline whats already underlined
return;
}
Font font = doc.getFont(as);
FontMetrics fontMetrics = pane.getFontMetrics(font);
try {
Rectangle rectangle = new Rectangle(
pane.modelToView(elem.getStartOffset()).x,
pane.modelToView(elem.getStartOffset()).y,
fontMetrics.stringWidth(doc.getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset())),
fontMetrics.getHeight());
if (h != null && offset < elem.getEndOffset() - 1 && rectangle.contains(e.getPoint())) {
Style hlStyle = doc.getStyle("regularBlue-findtype"); // NOI18N
pane.putClientProperty(PREV_HIGHLIGHT_ATTRIBUTES, as.copyAttributes());
doc.setCharacterAttributes(h.startOffset, h.endOffset - h.startOffset, hlStyle, true);
// doc.setCharacterAttributes(h.startOffset, h.endOffset - h.startOffset, as.copyAttributes(), true);
pane.putClientProperty(PREV_HIGHLIGHT_PROPERTY, h);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}
public static JTextPane createHmtlJTextPane(ImageInfoReader imageInfoReader) {
JTextPane textPane = createJTextPane(imageInfoReader);
textPane.setContentType("text/html");
textPane.putClientProperty(JTextPane.HONOR_DISPLAY_PROPERTIES, true);
return textPane;
}