我RSyntaxTextArea
使用 SwingNode 在 JavaFX 中工作,但似乎无法让代码完成工作。因此,为了消除任何与 JavaFX 相关的问题,我实现了一个没有 JavaFX 的仅 Java/Swing 版本。这是代码:
package com.mystuff.swingtest;
import java.awt.*;
import javax.swing.*;
import org.fife.ui.autocomplete.*;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;
class AutoCompleteDemo extends JFrame {
public AutoCompleteDemo() {
JPanel contentPane = new JPanel(new BorderLayout());
RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
contentPane.add(new RTextScrollPane(textArea));
CompletionProvider provider = createCompletionProvider();
AutoCompletion ac = new AutoCompletion(provider);
ac.install(textArea);
setContentPane(contentPane);
setTitle("AutoComplete Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
/**
* Create a simple provider that adds some Java-related completions.
*/
private CompletionProvider createCompletionProvider() {
DefaultCompletionProvider provider = new DefaultCompletionProvider();
// Add completions for all Java keywords. A BasicCompletion is just
// a straightforward word completion.
provider.addCompletion(new BasicCompletion(provider, "abstract"));
provider.addCompletion(new BasicCompletion(provider, "assert"));
provider.addCompletion(new BasicCompletion(provider, "break"));
provider.addCompletion(new BasicCompletion(provider, "case"));
// ... etc ...
provider.addCompletion(new BasicCompletion(provider, "transient"));
provider.addCompletion(new BasicCompletion(provider, "try"));
provider.addCompletion(new BasicCompletion(provider, "void"));
provider.addCompletion(new BasicCompletion(provider, "volatile"));
provider.addCompletion(new BasicCompletion(provider, "while"));
// Add a couple of "shorthand" completions. These completions don't
// require the input text to be the same thing as the replacement text.
provider.addCompletion(new ShorthandCompletion(provider, "sysout",
"System.out.println(", "System.out.println("));
provider.addCompletion(new ShorthandCompletion(provider, "syserr",
"System.err.println(", "System.err.println("));
return provider;
}
public static void main(String[] args) {
// Instantiate GUI on the EDT.
SwingUtilities.invokeLater(() -> {
try {
String laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
} catch (Exception e) { /* Never happens */ }
new AutoCompleteDemo().setVisible(true);
});
}
}
但是当我输入sysout
并点击时,crtl+SPACE
什么也没有发生。没有弹出窗口或任何东西。我从 AutoComplete 对象中检查了击键组合,它显示“ctrl press SPACE”,所以我知道组合键是正确的。
我正在使用 Mac。尝试了 COMMAND,ALT 而不是 Control,但仍然没有。有任何想法吗?
更新 1
如果我按该顺序SPACE
按住ctrl
,弹出窗口会闪烁,当我松开时ctrl
,如果我正确抓住它,弹出窗口会保持亮起
更新 2
这导致我尝试更改触发键。我还没有想出这个ctrl-whatever
组合,但我试过了
ac.setTriggerKey(KeyStroke.getKeyStroke('\t'));
现在按下TAB
编辑器会弹出没有闪烁的弹出窗口。因此,如果我能找到正确的组合键,我可能能够解决最初的问题KeyStroke
。