我正在使用
如果我注释掉这行代码:
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))
。 - trashgodFont.MONOSPACED
仍然返回了"Monospaced"
:) - Eng.FouadUIManager.setLookAndFeel()
从静态初始化程序中移出并放在EventQueue.invokeLater()
之前是否有帮助? - trashgod