HTML打开时的JTextPane setForeground

时间:2013-11-11 09:01:42

标签: java swing jtextpane

我有一个JTextPane我希望有时会更改它foreground。问题是,如果要将ContentType JTextPane切换为text/html,我的通话jTextPane.setForeground(myColor)就不会出现。 我该如何解决?

SSCCE:

public class TextAreaTest {
    public static void main(String[] a) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTextPane textArea = new JTextPane();
        textArea.setEditable(false);
        textArea.setFocusable(false);

        textArea.setText("<html>First line text<br>Second line text</html>");

        final JPanel panel = new JPanel(new FlowLayout());

        final JCheckBox htmlOnOff = new JCheckBox("HTML");
        htmlOnOff.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setContentType(htmlOnOff.isSelected() ? "text/html" : "text/plane");
                textArea.setText("<html>First line text<br>Second line text</html>");
            }
        });

        JToggleButton buttonRed = new JToggleButton("Red");
        buttonRed.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setForeground(Color.RED);
            }
        });

        JToggleButton buttonGreen = new JToggleButton("Green");
        buttonGreen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setForeground(Color.GREEN);
            }
        });

        JToggleButton buttonBlue = new JToggleButton("Blue");
        buttonBlue.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setForeground(Color.BLUE);
            }
        });

        panel.add(htmlOnOff);
        panel.add(buttonRed);
        panel.add(buttonGreen);
        panel.add(buttonBlue);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(buttonRed);
        buttonGroup.add(buttonBlue);
        buttonGroup.add(buttonGreen);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
        contentPane.add(panel, BorderLayout.NORTH);
        contentPane.add(textArea, BorderLayout.CENTER);

        frame.setContentPane(contentPane);
        frame.setUndecorated(true);
        Dimension size = new Dimension(275, 80);
        frame.setSize(size);
        frame.setPreferredSize(size);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
                System.out.println(frame.getSize());
            }
        });
    }
}

更新#1 我注意到JLabel能够做我想做的事。但我不能用它:(

1 个答案:

答案 0 :(得分:0)

谢谢大家,但我自己也找到了答案:

StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, myColor);
doc.setParagraphAttributes(0, doc.getLength(), attrs, false);
textPane.setDocument(doc);