【问题标题】:JTextPane Dynamic Indent !!! SituationJTextPane 动态缩进!!!情况
【发布时间】:2020-05-22 11:59:35
【问题描述】:

我尝试动态更新 Jtextpane 中的左缩进。但我不能!这是我尝试过的!

DefaultStyledDocument document = (DefaultStyledDocument) textpane.getDocument();
Element element = document.getCharacterElement(start);
AttributeSet attribs = element.getAttributes();
attribs.containsAttribute(StyleConstants.LeftIndent, 20);
document.setCharacterAttributes(start, length, attribs, true);

【问题讨论】:

  • 请附上你得到的结果。我无法理解你的问题。您可能需要StyleConstants.FirstLineIndent。见documentation
  • 感谢 Vince Emigh.. 结果是当我运行这些代码时,它只是在我设置之前删除了所有属性。它只是将所有内容设为默认设置..我想像在 Dreamweaver 或 netbeans 或 Visual Studio 中一样更新左缩进 ..你知道按钮缩进块。

标签: java swing jtextarea jtextpane


【解决方案1】:

以下是如何将Action 添加到JButton 以缩进一段文本:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class TextPaneIndent extends JPanel
{
    public TextPaneIndent()
    {
        setLayout( new BorderLayout() );

        JTextPane textPane = new JTextPane();
        textPane.setText("one\ntwo\nthree\nfour");
        textPane.setPreferredSize( new Dimension(200, 220) );
        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
        add( scrollPane );

        //  Create a Button panel

        JPanel buttons = new JPanel();
        add(buttons, BorderLayout.PAGE_END);

        //  Add Indent button

        JButton indent = new JButton( new LeftIndentAction("Indent", 10) );
        add( indent );

        //  Add Outdent button

        JButton outdent = new JButton( new LeftIndentAction("Outdent", -10) );
        add( outdent );
    }

    class LeftIndentAction extends StyledEditorKit.StyledTextAction
    {
        private float value;

        public LeftIndentAction(String name, float value)
        {
            super(name);
            this.value = value;
        }

        public void actionPerformed(ActionEvent e)
        {
            JEditorPane editor = getEditor(e);

            if (editor != null)
            {
                StyledDocument doc = getStyledDocument( editor );
                int offset = editor.getCaretPosition();
                Element paragraph = doc.getParagraphElement( offset );
                AttributeSet as = paragraph.getAttributes();
                float indent = StyleConstants.getLeftIndent( as );
                indent += value;

                MutableAttributeSet attr = new SimpleAttributeSet();
                StyleConstants.setLeftIndent(attr, indent);
                setParagraphAttributes(editor, attr, false);
            }
        }
    }

    public static void main(String[] args)
    {
        TextPaneIndent frame = new TextPaneIndent();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TextPaneIndent());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

【讨论】:

  • 其实我只需要中间部分...复制样式文档属性并只增加减少左缩进。非常感谢 camickr 所做的努力,但您的代码将删除所有已定义的属性。但我猜“MutableAttributeSet attr = new SimpleAttributeSet();”在这里,如果我可以复制元素之前的属性,它将完成..我会接受您的回答..再次感谢您的回复:)
猜你喜欢
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多