简体   繁体   English

如何为用Java编写的文本编辑器实现自动完成功能?

[英]How to implement an auto-complete for a text editor written in java?

I'm trying to create a text editor like program for coding in mips assembly using java , the point i got mixed up alittle was the part i was trying to provide a Control-Space feature like that of eclipse's.For example when the user enters add $s1 , then presses ctrl+Space , i would replace this string: "add $s1 , $s2 , $s3" with what he has typed! 我正在尝试创建一个文本编辑器,例如使用java在mips汇编中进行编码的程序,我有点困惑,这就是我试图提供类似eclipse的Control-Space功能的部分,例如当用户输入add $s1 ,然后按ctrl + Space,我将用他输入的内容替换此字符串: "add $s1 , $s2 , $s3" ( that's an example .. i know add can be of immediate type instructions :D ), I'm using a hashmap to bind key strokes to what will actually happen in my JTextPane , sth like : (这是一个例子..我知道add可以是立即类型的指令:D),我正在使用哈希映射将击键绑定到JTextPane中实际发生的事情,例如:

InputMap inputMap = textPane.getInputMap();
KeyStroke key = KeyStroke.getKeyStroke('some keys') ;
inputMap.put(key, 'some action') ;

well the problem is , when i want to implement Ctrl+Space for my program , i am using this : 很好的问题是,当我想为程序实现Ctrl + Space时,我正在使用以下代码:

key = KeyStroke.getKeyStroke(Event.CTRL_MASK , KeyEvent.VK_SPACE) ;
inputMap.put(key, DefaultEditorKit.insertContentAction);

but where can I select , what string should be inserted instead of the already typed part of the pattern ? 但是我在哪里可以选择,应该插入什么字符串而不是模式的已键入部分? other words , where does the insertContentAction bring it strings from ? 换句话说,insertContentAction将字符串从何处带出? how can i define what String should be replaced ? 我如何定义应替换的字符串? or even is there any other better ways for replacing , when Ctrl+Space has been pressed ? 甚至在按下Ctrl + Space时还有其他更好的替换方法吗? (what im asking for is a way to insert the string pattern into a jTextPane , not the algorithm to implement the pattern matching ) (即时通讯要求的是将字符串模式插入jTextPane的方法,而不是用于实现模式匹配的算法)
Thanks in advance :) 提前致谢 :)

You can get the Document of the text pane to make tightly controlled changes such as these. 您可以获取文本窗格的Document以进行严格控制的更改,例如这些。 The Document also has some useful events of its own that you can listen for. Document还具有一些您可以监听的有用事件。

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

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