1

JTextPaneを使用してテキストエディタを作成しようとしていますが、選択したテキストの色を設定できません。これが思い付くことができる最高のものです(しかし、明らかに、機能していません):

    JMenuItem button = new JMenuItem("Set Color");
    toolbar.add(button);

    button.addActionListener(new ActionListener( ) {
        public void actionPerformed(ActionEvent e) {
            Color c = JColorChooser.showDialog(frame,"Choose a color", getBackground());
            textPane.getSelectedText().StyledEditorKit.ForegroundAction("color",c);
        }
    });

それを機能させる方法について何か提案はありますか?それともそれを行うためのより良い方法ですか?

ありがとう

4

1 に答える 1

2

getSelectedText()選択したテキストを含む通常の文字列を返すだけです。テキストの属性を変更するために使用することはできません。

SimpleAttributeSetまず、とを使用StyleConstantsして色属性を生成し、それをテキストの選択した部分に適用します。

SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, c);
textPane.setCharacterAttributes(attr, false);
于 2010-11-20T19:33:37.710 に答える