gpt4 book ai didi

java - 在 Java 中将可编辑的 JTextArea 包裹在不可编辑的文本周围

转载 作者:行者123 更新时间:2023-11-30 04:08:38 24 4
gpt4 key购买 nike

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

据我所知,唯一可以做到这一点的方法是:Make parts of a JTextArea non editable (not the whole JTextArea!)

还有其他方法吗?

最佳答案

Just the beginning

然后你可以这样做:

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);
}
}

关于java - 在 Java 中将可编辑的 JTextArea 包裹在不可编辑的文本周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com