【发布时间】:2012-05-11 00:54:46
【问题描述】:
我正在使用Highlighter.HighlightPainter 界面来突出显示文本区域的行。我使用了这个网站的源代码:Line Painter。它工作得很好,但是当我使用org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel 来装饰 GUI 时,出现了一个问题。每当我将文本区域的字体更改为Monospaced 时,由于某种原因不会调用Highlighter.HighlightPainter 的paint() 方法。这是一个示例代码:
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Shape;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
public class TestFrame extends JFrame implements Highlighter.HighlightPainter
{
private static final long serialVersionUID = 1L;
static
{
try
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.setLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel());
}
catch(Exception e)
{
e.printStackTrace();
}
}
public TestFrame() throws BadLocationException
{
super("The title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea txt = new JTextArea(10, 30);
txt.getHighlighter().addHighlight(0, 0, this);
txt.setFont(new Font("Monospaced", Font.PLAIN, 12));
JPanel container = (JPanel) getContentPane();
container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
container.add(txt);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
new TestFrame().setVisible(true);
}
catch(BadLocationException e)
{
e.printStackTrace();
}
}
});
}
@Override
public void paint(Graphics g, int p0, int p1, Shape bounds, JTextComponent c)
{
System.out.println("paint() is invoked!");
}
}
如果我评论这一行:
txt.setFont(new Font("Monospaced", Font.PLAIN, 12));
paint() 将被调用。有没有办法解决这个问题?
【问题讨论】:
-
顺便说一句,考虑
txt.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12))。 -
@trashgod 感谢您的提示!仍然
Font.MONOSPACED返回"Monospaced":) -
将
UIManager.setLookAndFeel()移出静态初始化程序并放在EventQueue.invokeLater()之前是否有帮助? -
@trashgod 好吧,它现在可以工作了。感谢您的提示:)