Java 如何使JTextArea的结尾可编辑

Java 如何使JTextArea的结尾可编辑,java,swing,jtextarea,Java,Swing,Jtextarea,有没有办法使JTextArea的结尾可编辑,并使已打印到其中的内容不可编辑 我的意思是,如果我已经将“Hello World”写进了一个JTextArea,我怎么能让用户在“Hello World”之后输入他们想要的任何内容,但他们不能在此之前输入或删除已经打印的文本 下面是一个小程序来演示我的烦恼 public class Test { public static void main(String[] args) { //Here I create a simple J

有没有办法使JTextArea的结尾可编辑,并使已打印到其中的内容不可编辑

我的意思是,如果我已经将“Hello World”写进了一个JTextArea,我怎么能让用户在“Hello World”之后输入他们想要的任何内容,但他们不能在此之前输入或删除已经打印的文本

下面是一个小程序来演示我的烦恼

public class Test {
    public static void main(String[] args) {
        //Here I create a simple JFrame with JTextArea
        JTextArea textArea = new JTextArea();
        JFrame frame = new JFrame();
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setSize(250, 250);
        textArea.setEditable(true);
        textArea.setVisible(true);
        frame.add(textArea);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        /*Here I print "Hello World" onto the text area.. after the ">>" I want the
        the user to be able to type whatever they want.. however I don't want them
        to be able to edit the "Hello World"*/
        textArea.append("Hello World\n>>");
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}
在本例中,用户可以输入他们想要的任何文本。。这就是我想要的。。但是,他们也可以编辑我使用append打印的文本。。但我不想


我如何解决这个问题?

是的,文档过滤器可以工作。创建一个仅允许在文档末尾添加文本的选项,即偏移量等于文档长度时。同时完全禁用删除方法。大概是这样的:

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class MyFilter extends DocumentFilter {
    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
            throws BadLocationException {
        // only insert text if at the end of the document
        // if offset == document length
        if (offset == fb.getDocument().getLength()) {
            super.insertString(fb, offset, string, attr);
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
            throws BadLocationException {
        // only replace text if at the end of the document
        // if offset == document length
        if (offset == fb.getDocument().getLength()) {
            super.replace(fb, offset, length, text, attrs);
        }
    }

    @Override
    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
        // do nothing. Totally inactivate this
    }
}
你可以这样测试它:

import javax.swing.*;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class LimitedTextArea extends JPanel {
    private JTextArea textArea = new JTextArea(15, 50);

    public LimitedTextArea() {
        // get textArea's Document and cast to PlainDocument:
        PlainDocument document = (PlainDocument) textArea.getDocument();
        // set the document's filter with "MyFilter"
        document.setDocumentFilter(new MyFilter());

        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);
    }

    private static void createAndShowGui() {
        LimitedTextArea mainPanel = new LimitedTextArea();

        JFrame frame = new JFrame("LimitedTextArea");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

是的,文档过滤器可以工作。创建一个仅允许在文档末尾添加文本的选项,即偏移量等于文档长度时。同时完全禁用删除方法。大概是这样的:

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class MyFilter extends DocumentFilter {
    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
            throws BadLocationException {
        // only insert text if at the end of the document
        // if offset == document length
        if (offset == fb.getDocument().getLength()) {
            super.insertString(fb, offset, string, attr);
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
            throws BadLocationException {
        // only replace text if at the end of the document
        // if offset == document length
        if (offset == fb.getDocument().getLength()) {
            super.replace(fb, offset, length, text, attrs);
        }
    }

    @Override
    public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
        // do nothing. Totally inactivate this
    }
}
你可以这样测试它:

import javax.swing.*;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class LimitedTextArea extends JPanel {
    private JTextArea textArea = new JTextArea(15, 50);

    public LimitedTextArea() {
        // get textArea's Document and cast to PlainDocument:
        PlainDocument document = (PlainDocument) textArea.getDocument();
        // set the document's filter with "MyFilter"
        document.setDocumentFilter(new MyFilter());

        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);
    }

    private static void createAndShowGui() {
        LimitedTextArea mainPanel = new LimitedTextArea();

        JFrame frame = new JFrame("LimitedTextArea");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

您还可以使用
导航过滤器

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

public class NavigationFilterPrefix extends NavigationFilter
{
    private int prefixLength;
    private Action deletePrevious;

    public NavigationFilterPrefix(int prefixLength, JTextComponent component)
    {
        this.prefixLength = prefixLength;
        deletePrevious = component.getActionMap().get("delete-previous");
        component.getActionMap().put("delete-previous", new BackspaceAction());
        component.setCaretPosition(prefixLength);
    }

    @Override
    public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.setDot(Math.max(dot, prefixLength), bias);
    }

    @Override
    public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.moveDot(Math.max(dot, prefixLength), bias);
    }

    class BackspaceAction extends AbstractAction
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = (JTextComponent)e.getSource();

            if (component.getCaretPosition() > prefixLength)
            {
                deletePrevious.actionPerformed( null );
            }
        }
    }

    private static void createAndShowUI()
    {
        JTextField textField = new JTextField("Prefix_", 20);
        textField.setNavigationFilter( new NavigationFilterPrefix(7, textField) );

        JFrame frame = new JFrame("Navigation Filter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textField);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
这将允许用户编辑他们添加到文本字段的文本

这将阻止选择固定文本


有关更高级的功能,请查看,它允许您保护文档的多个区域不被更改。

您也可以使用
导航筛选器

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

public class NavigationFilterPrefix extends NavigationFilter
{
    private int prefixLength;
    private Action deletePrevious;

    public NavigationFilterPrefix(int prefixLength, JTextComponent component)
    {
        this.prefixLength = prefixLength;
        deletePrevious = component.getActionMap().get("delete-previous");
        component.getActionMap().put("delete-previous", new BackspaceAction());
        component.setCaretPosition(prefixLength);
    }

    @Override
    public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.setDot(Math.max(dot, prefixLength), bias);
    }

    @Override
    public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias)
    {
        fb.moveDot(Math.max(dot, prefixLength), bias);
    }

    class BackspaceAction extends AbstractAction
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JTextComponent component = (JTextComponent)e.getSource();

            if (component.getCaretPosition() > prefixLength)
            {
                deletePrevious.actionPerformed( null );
            }
        }
    }

    private static void createAndShowUI()
    {
        JTextField textField = new JTextField("Prefix_", 20);
        textField.setNavigationFilter( new NavigationFilterPrefix(7, textField) );

        JFrame frame = new JFrame("Navigation Filter Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(textField);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
这将允许用户编辑他们添加到文本字段的文本

这将阻止选择固定文本


有关更高级的功能,请查看,它允许您保护文档的多个区域不被更改。

文档过滤器可能会起作用。我会用它做实验。也演示了;)文档过滤器可能会起作用。我会用它做实验。也演示了;)