首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用等宽字体时Substance UI的HighlightPainter问题

使用等宽字体时Substance UI的HighlightPainter问题
EN

Stack Overflow用户
提问于 2012-05-11 08:54:46
回答 3查看 640关注 0票数 3

我使用Highlighter.HighlightPainter界面来突出显示文本区的行。我使用了这个网站上的源代码:Line Painter。它工作得很好,但是当我使用org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel来装饰图形用户界面时,出现了一个问题。每当我将文本区域的字体更改为Monospaced时,由于某种原因,Highlighter.HighlightPainterpaint()方法不会被调用。下面是一个示例代码:

代码语言:javascript
复制
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!");
    }
}

如果我注释此行:

代码语言:javascript
复制
txt.setFont(new Font("Monospaced", Font.PLAIN, 12));

将调用paint()。有什么办法可以解决这个问题吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-11 11:48:32

1) @Eng.Fouad这是在这里提到的n_times (@camickr,@StanislavL),对于样式和高亮显示的测试,使用支持它的JTextComponent

2) @Eng.Fouad JTextArea的输出是正确的

来自代码

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextPaneHighlighting {

    private static final long serialVersionUID = 1L;
    private Highlighter.HighlightPainter cyanPainter;
    private Highlighter.HighlightPainter redPainter;

    public TextPaneHighlighting() {
        JFrame frame = new JFrame();
        JTextPane textPane = new JTextPane();
        textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\n");
        //textPane.setFont(new Font("Monospaced", Font.PLAIN, 12)); // uncommnent
        JScrollPane scrollPane = new JScrollPane(textPane);
        frame.add(scrollPane, BorderLayout.CENTER);//  Highlight some text
        cyanPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.cyan);
        redPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.red);
        try {
            textPane.getHighlighter().addHighlight(0, 3, DefaultHighlighter.DefaultPainter);
            textPane.getHighlighter().addHighlight(8, 14, cyanPainter);
            textPane.getHighlighter().addHighlight(19, 24, redPainter);
        } catch (BadLocationException ble) {
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300, 200));
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                try {
                    UIManager.setLookAndFeel(new org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel());
                } catch (UnsupportedLookAndFeelException ex) {
                }
                TextPaneHighlighting tph = new TextPaneHighlighting();
            }
        });
    }
}

3)正如@ trashgod正确提到的那样,永远不要在没有invokeLater的情况下设置任何实质内容,永远不要,无论外观和感觉如何敏感,也许在这个时刻字体并不重要,也许不重要

4)简单的JTextArea有一些字体和外观的问题,也许有自己的高亮概念作为渲染器(对不起我懒惰的读wake_up的API )和实质,对于渲染器的概念,你必须使用SubstanceRenderer而不是XxxRenderer,然后所有的格式如你所期望的那样唤醒

代码语言:javascript
复制
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class Fonts implements Runnable {

    private String[] fnt;
    private JFrame frm;
    private JScrollPane jsp;
    private JTextPane jta;
    private int width = 450;
    private int height = 300;
    private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    private StyledDocument doc;
    private MutableAttributeSet mas;
    private int cp = 0;
    private Highlighter.HighlightPainter cyanPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.cyan);
    private Highlighter.HighlightPainter redPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.red);
    private Highlighter.HighlightPainter whitePainter = new DefaultHighlighter.DefaultHighlightPainter(Color.white);
    private int _count = 0;
    private int _lenght = 0;

    public Fonts() {
        jta = new JTextPane();
        doc = jta.getStyledDocument();
        jsp = new JScrollPane(jta);
        jsp.setPreferredSize(new Dimension(height, width));
        frm = new JFrame("awesome");
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLayout(new BorderLayout());
        frm.add(jsp, BorderLayout.CENTER);
        frm.setLocation(100, 100);
        frm.pack();
        frm.setVisible(true);
        jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        fnt = ge.getAvailableFontFamilyNames();
        mas = jta.getInputAttributes();
        new Thread(this).start();
    }

    @Override
    public void run() {
        for (int i = 0; i < fnt.length; i++) {
            StyleConstants.setBold(mas, false);
            StyleConstants.setItalic(mas, false);
            StyleConstants.setFontFamily(mas, fnt[i]);
            StyleConstants.setFontSize(mas, 16);
            dis(fnt[i]);
            try {
                Thread.sleep(75);
            } catch (Exception e) {
                e.printStackTrace();
            }
            StyleConstants.setBold(mas, true);
            dis(fnt[i] + " Bold");
            try {
                Thread.sleep(75);
            } catch (Exception e) {
                e.printStackTrace();
            }
            StyleConstants.setItalic(mas, true);
            dis(fnt[i] + " Bold & Italic");
            try {
                Thread.sleep(75);
            } catch (Exception e) {
                e.printStackTrace();
            }
            StyleConstants.setBold(mas, false);
            dis(fnt[i] + " Italic");
            try {
                Thread.sleep(75);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        jta.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    public void dis(String s) {
        _count++;
        _lenght = jta.getText().length();
        try {
            doc.insertString(cp, s, mas);
            doc.insertString(cp, "\n", mas);
        } catch (Exception bla_bla_bla_bla) {
            bla_bla_bla_bla.printStackTrace();
        }
        if (_count % 2 == 0) {
            try {
                jta.getHighlighter().addHighlight(1, _lenght - 1, cyanPainter);
            } catch (BadLocationException bla_bla_bla_bla) {
            }
        } else if (_count % 3 == 0) {
            try {
                jta.getHighlighter().addHighlight(1, _lenght - 1, redPainter);
            } catch (BadLocationException bla_bla_bla_bla) {
            }
        } else {
            try {
                jta.getHighlighter().addHighlight(1, _lenght - 1, whitePainter);
            } catch (BadLocationException bla_bla_bla_bla) {
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                JDialog.setDefaultLookAndFeelDecorated(true);
                try {
                    UIManager.setLookAndFeel(new org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel());
                } catch (UnsupportedLookAndFeelException ex) {
                }
                Fonts fs = new Fonts();
            }
        });
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-05-11 10:49:15

这个问题有一个解决方案,就是在调用UIManager.setLookAndFeel()之前创建文本区域并为其设置字体。

票数 3
EN

Stack Overflow用户

发布于 2012-05-11 12:14:04

仅供参考,这里是对LinePainter的快速测试

代码语言:javascript
复制
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;

/**
 * @see http://stackoverflow.com/q/10544046/230513
 * @see http://tips4java.wordpress.com/2008/10/29/line-painter/
 */
public class LinePainterTest extends JPanel {

    public LinePainterTest() {
        JTextPane textPane = new JTextPane();
        textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
        try {
            textPane.getDocument().insertString(0, "One\nTwo\nThree", null);
        } catch (BadLocationException ex) {
            ex.printStackTrace(System.err);
        }
        LinePainter painter = new LinePainter(textPane);
        this.add(textPane);
    }

    private void display() {
        JFrame f = new JFrame("LinePainterTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(
            "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        JFrame.setDefaultLookAndFeelDecorated(true);
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new LinePainterTest().display();
            }
        });
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10544046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档