这里有人使用合金外观吗?我正面临一个带有抗锯齿和 JTextComponents 的奇怪错误。Alloy 默认情况下根本不使用抗锯齿,所以我必须通过创建自己的 UI 类来强制它。这在大多数情况下都可以正常工作,但某些字符会对抗锯齿造成严重破坏。
例如,如果将 Alloy 设置为外观,并且我将一些希伯来语文本插入到 JTextComponent 中,例如:שלום, מה שלומך שמי הוא האקזיד',整个 JTextComponent 突然失去抗锯齿 - 永久。
奇怪的是,我什至没有扩展 AlloyTextPaneUI,而是扩展了 BasicTextPaneUI 来进行抗锯齿,所以我对 Alloy 出现在图片中的位置感到困惑(其他 Look and Feels 似乎工作得很好)。
我很难追踪这个错误......有人遇到同样的问题吗?
这是一个演示问题的简短示例:
import com.incors.plaf.alloy.AlloyLookAndFeel;
import com.incors.plaf.alloy.themes.glass.GlassTheme;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import java.awt.*;
public class Scrap {
static {
// NOTE: You need a license code for Alloy!
AlloyLookAndFeel.setProperty("alloy.licenseCode", "your license here");
UIManager.put("TextPaneUI", MyTextPaneUI.class.getName());
try {
UIManager.setLookAndFeel(new AlloyLookAndFeel(new GlassTheme()));
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
// With system Look and Feel everything works just fine...
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (InstantiationException e) {
// e.printStackTrace();
// } catch (IllegalAccessException e) {
// e.printStackTrace();
// } catch (UnsupportedLookAndFeelException e) {
// e.printStackTrace();
// }
}
public static void main(final String args[]) {
JTextPane text = new JTextPane();
text.setFont(new Font("Monospaced", Font.PLAIN, 14));
StyledDocument doc = text.getStyledDocument();
try {
doc.insertString(doc.getLength(), "Here's some regular text. Nice and smooth with anti-aliasing.\n\n", null);
doc.insertString(doc.getLength(), "Here's some more text Blaa Blaa Blaaa. Lorem ipsum... now try to uncomment the Hebrew text.\n\n", null);
// Try to uncomment this line and the anti-aliasing breaks for the whole JTextPane
// doc.insertString(doc.getLength(), "שלום, מה שלומך שמי הוא האקזידן\n\n", null);
// Here's another strange glyph that breaks the anti-aliasing
// doc.insertString(doc.getLength(), "ಠ", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(text);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scroll, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static class MyTextPaneUI extends BasicTextPaneUI {
@Override
protected void paintSafely(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
super.paintSafely(g);
}
public static ComponentUI createUI(JComponent c) {
return new MyTextPaneUI();
}
}
}