这里有人使用合金外观吗?我在使用抗锯齿和JTextComponents时遇到一个奇怪的错误。默认情况下,Alloy根本不使用抗锯齿功能,因此我必须通过创建自己的UI类来强制使用抗锯齿功能。在大多数情况下,这可以正常工作,但是某些字符会对消除锯齿造成严重破坏。
例如,如果将Alloy设置为外观,并且我将一些希伯来语文本插入JTextComponent中,例如:
奇怪的是,我什至没有扩展AlloyTextPaneUI,而是通过BasicTextPaneUI进行抗锯齿,因此我对合金出现在图片中的位置感到困惑(其他外观似乎很好)。
我很难追踪这个错误...有人遇到同样的问题吗?
这是一个演示问题的简短示例:
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();
}
}
}
最佳答案
经过一些非常令人沮丧的实验后,我将其修复。.我仍然不确定是什么导致了它,但是这是我修复它的方法。合金中的UIDefaults中显然有一个键丢失/损坏(我不知道是哪个键)。因此,我将所有默认设置(从初始外观和感觉)添加到Alloy属性。这是一种快速而肮脏的解决方案(我唯一的问题是菜单变得不透明),但足以满足我的需求。也许其他人发现它也有帮助。
AlloyLookAndFeel laf = new AlloyLookAndFeel(theme) {
@Override
public UIDefaults getDefaults() {
UIDefaults defs = new UIDefaults();
defs.putAll(UIManager.getLookAndFeelDefaults());
initClassDefaults(defs);
initSystemColorDefaults(defs);
initComponentDefaults(defs);
defs.put("Menu.opaque", true);
return defs;
}
};