Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中围绕不可编辑文本包装可编辑JTextArea_Java_Swing - Fatal编程技术网

在Java中围绕不可编辑文本包装可编辑JTextArea

在Java中围绕不可编辑文本包装可编辑JTextArea,java,swing,Java,Swing,我正在使用Swing生成命令提示符。我正在为输入使用可编辑的JTextArea,但我不想在输入之前使用可编辑的区域(C:\Windows\system32>) 我所知道的唯一可以做到这一点的方法是: 还有别的办法吗 刚刚开始 然后,您可以执行以下操作: import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class NavigationFilterPrefixWithBackspace

我正在使用Swing生成命令提示符。我正在为输入使用可编辑的JTextArea,但我不想在输入之前使用可编辑的区域(C:\Windows\system32>)

我所知道的唯一可以做到这一点的方法是:

还有别的办法吗

刚刚开始

然后,您可以执行以下操作:

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

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

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

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

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

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

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

    public static void main(String args[]) throws Exception {

        JTextField textField = new JTextField("Prefix_", 20);
        textField.setNavigationFilter( new NavigationFilterPrefixWithBackspace(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);
    }
}

我不确定您所说的是在文本区域的开头,还是在文本区域的每一行上都没有可编辑的文本。根据不同的要求,答案会有所不同。