简体   繁体   English

如何仅使JTextArea(+ JScrollPane)的下一行可编辑

[英]How to make only the next line of JTextArea (+JScrollPane) editable

So im creating a server, and that works great, however I am a bit stuck on the GUI. 因此,即时通讯创建了一个服务器,并且效果很好,但是我在GUI上有些卡住了。 You see, I would like it to look just like Command Prompt, where only the next line is editable, and it does not let you delete any other text. 您会看到,我希望它看起来像“命令提示符”,仅下一行是可编辑的,并且不允许您删除任何其他文本。 So right now I have: 所以现在我有:

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);

Then the frame stuff... 然后是框架的东西...

    f.setTitle("Server");
    f.setBounds(ss.width - 600, 50, 550, 350);
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//added window listener so closes socket connection first
    f.setAlwaysOnTop(true);

Then adding it: 然后添加它:

    f.add(sc);
    jt.setBackground(Color.BLACK);
    jt.setForeground(Color.WHITE);
    //jt.setEditable(false);

Finally, the method I use to output to the TextArea: 最后,我用来输出到TextArea的方法:

public static void append(String text) {
    jt.append(text);
    jt.append("\n\n"+System.getProperty("user.name")+" / "+getIp()+" > ");
    jt.setCaretPosition(jt.getDocument().getLength());
}

Now I need to assign a String to what the user types into the JTextArea after they press enter:>? 现在,我需要为用户按回车后在JTextArea中键入的内容分配一个字符串:

jt.addActionListener(...{
    public void ActioEvent(ActionEvent e){
        String text = JTextArea.getLines().getLastLine().getText().replace(System.getProperty("user.name")+" / "+getIp()+"       > ", "");
    }
});

Maybe something like that? 也许是这样吗? Then I need to have it so that only the part after the ">" is editable? 然后,我需要它以便仅“>”之后的部分是可编辑的?

The way to do this is with a DocumentFilter . 做到这一点的方法是使用DocumentFilter This is a fairly obscure and little-used part of Java, and is far from easy to use. 这是Java相当晦涩且很少使用的部分,并且远非易用。 However it allows you to insert a DocumentFilter between the UI (where rich text content is edited) and the underlying model (the content). 但是,它允许您在UI(用于编辑富文本内容的位置)和基础模型(内容)之间插入DocumentFilter。 You pass all the 'insert' and 'remove' operations through the filter, which can either accept, refuse or modify them. 您通过过滤器传递所有“插入”和“删除”操作,它们可以接受,拒绝或修改它们。 You can code the filter to only permit modifications to the command line, and not to the prompt. 您可以对过滤器进行编码,以仅允许对命令行进行修改,而不允许对提示符进行修改。

As I say, this is a pretty hard piece of coding, and the Document/DocumentFilter structure has a lot of complexity that your particular application doesn't need. 就像我说的那样,这是一段非常困难的编码,而Document / DocumentFilter结构具有您特定应用程序不需要的很多复杂性。 However it does provide you with the facilities you need. 但是,它确实为您提供了所需的设施。

There is a tutorial in the standard Java doc pages, but not an advanced one, and very few examples that I know of are out there on the web. 标准Java文档页面中有一个教程,而高级教程中没有,而网络上我所知道的例子很少。

ProtectedTextComponent (thanks camickr) provides an example of how to do something similar. ProtectedTextComponent (感谢camickr)提供了有关如何执行类似操作的示例。

Use a Collection a JTextField . 使用Collection JTextField
Let the user type on a JTextField , and once he presses enter , move the control to the next JTextField while making the above JTextField uneditable and also remove the JScrollPane from it. 让用户在JTextField上键入内容,然后按enter ,将控件移至下一个JTextField同时使上述JTextField不可编辑,并从中删除JScrollPane

Hope this helps. 希望这可以帮助。

I also agree that the JTextArea/JTextField approach is the common and simpler approach. 我也同意JTextArea / JTextField方法是常见且更简单的方法。

However if you want to complicate your life a little then you can check out Protected Text Component which will do most of the logic for you. 但是,如果您想使自己的生活复杂一点,则可以查看受保护的文本组件 ,它将为您执行大部分逻辑。

The current implemtation of the ProtectedDocument only allows you to add protection to the Document, not remove it so the first thing you will need to do is add a method to "Clear" all the protect pieces of text. ProtectedDocument的当前实现仅允许您向文档添加保护,而不能删除它,因此您需要做的第一件事是添加一种方法来“清除”所有受保护的文本。 This is easy enough, you just clear the entries in the Map used by the class. 这很容易,您只需清除该类使用的Map中的条目即可。

Next you will need to replace the default "Enter" Action used by the JTextPane. 接下来,您将需要替换JTextPane使用的默认“ Enter”操作。 You do this by playing with the Key Bindings of the text area. 您可以通过使用文本区域的Key Bindings来实现。 See Key Bindings for some basic information. 有关一些基本信息,请参见键绑定 In your custom Action you would first need to invoke the newly created "clear(...)" method. 在您的自定义操作中,您首先需要调用新创建的“ clear(...)”方法。 Then you would add you text to the text area. 然后,您可以将文本添加到文本区域。 Finally you would protect all the text but the last "x" number of characters. 最后,您将保护所有文本,但最后一个“ x”个字符除外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM