有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java限制对JTextArea中某些行的访问?

我想制作一个JTextArea,用户不能在其中删除前一行。就像Windows中的命令提示符和Linux中的终端一样,您不能编辑前面的行

这就是我想到的,但似乎不起作用,我只能想出一个原因,但似乎不止一个原因

if(commandArea.getCaretPosition() < commandArea.getText().lastIndexOf("\n")){
        commandArea.setCaretPosition(commandArea.getText().lastIndexOf("\n"));
}

此代码块存在于此方法中:

private void commandAreaKeyPressed(java.awt.event.KeyEvent evt)

共 (1) 个答案

  1. # 1 楼答案

    您可以对JTextArea的文档使用DocumentFilter。仅允许在JTextArea中编辑最后一行的可运行工作示例:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.text.DocumentFilter.FilterBypass;
    
    public class MainClass {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("text area test");
            JPanel panelContent = new JPanel(new BorderLayout());
            frame.setContentPane(panelContent);
            UIManager.getDefaults().put("TextArea.font", UIManager.getFont("TextField.font")); //let text area respect DPI
            panelContent.add(createSpecialTextArea(), BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.setLocationRelativeTo(null); //center screen
            frame.setVisible(true);
        }
    
        private static JTextArea createSpecialTextArea() {
            final JTextArea textArea = new JTextArea("first line\nsecond line\nthird line");
            ((AbstractDocument)textArea.getDocument()).setDocumentFilter(new DocumentFilter() {
    
                private boolean allowChange(int offset) {
                    try {
                    int offsetLastLine = textArea.getLineCount() == 0 ? 0 : textArea.getLineStartOffset(textArea.getLineCount() - 1);
                    return offset >= offsetLastLine;
                    } catch (BadLocationException ex) {
                        throw new RuntimeException(ex); //should never happen anyway
                    }
                }
    
                @Override
                public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
                    if (allowChange(offset)) {
                        super.remove(fb, offset, length);
                    }
                }
    
                @Override
                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                    if (allowChange(offset)) {
                        super.replace(fb, offset, length, text, attrs);
                    }
                }
    
                @Override
                public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                    if (allowChange(offset)) {
                        super.insertString(fb, offset, string, attr);
                    }
                }
    
    
    
            });
            return textArea;
        }
    }
    

    它是如何工作的?JTextArea是一个文本控件,实际数据包含文档。文档允许侦听更改(DocumentListener),有些文档允许设置DocumentFilter以禁止更改。PlainDocument和DefaultStyledDocument都是从AbstractDocument扩展而来的,AbstractDocument允许设置过滤器

    请务必阅读java文档:

    我还推荐教程: