Java 在按下的按键区域听音乐
你好,我很高兴尝试从JTextArea构建自己的命令行 我想有更多的控制方式处理插入符号取决于什么键按下。我在JTextArea中添加了一个KeyListener。我的问题是:当按下并检测到向上键时,插入符号位置应该移动。如果我尝试在KeyListener中打印插入符号位置,我会得到上一个位置。捕捉按键事件后,将考虑新应用的位置。具体地说,假设getCaretPosition()方法返回15。如果我按下向上箭头,我仍然会从getCaretPosition()得到15。只有当我离开事件侦听器时,插入符号的位置才会改变 我想在KeyListener中获得新的插入符号位置。 当前的目标是检查新插入符号位置是否在换行提示之前。如果是这样,那么插入符号就不应该移动,因为我不希望这个区域可以修改。 所以我想得到一个新的插入符号位置,然后决定我是否要申请这个新位置。 我该怎么做 以下是我的代码块:Java 在按下的按键区域听音乐,java,swing,jtextarea,keylistener,caret,Java,Swing,Jtextarea,Keylistener,Caret,你好,我很高兴尝试从JTextArea构建自己的命令行 我想有更多的控制方式处理插入符号取决于什么键按下。我在JTextArea中添加了一个KeyListener。我的问题是:当按下并检测到向上键时,插入符号位置应该移动。如果我尝试在KeyListener中打印插入符号位置,我会得到上一个位置。捕捉按键事件后,将考虑新应用的位置。具体地说,假设getCaretPosition()方法返回15。如果我按下向上箭头,我仍然会从getCaretPosition()得到15。只有当我离开事件侦听器时,插
public MethodCommandLine() {
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
mainFrame.setLocation(200, 200);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prompt = new JTextArea(PROMPT);
prompt.setCaretPosition(PROMPT.length());
lastCmdPosition = prompt.getCaretPosition();
prompt.setPreferredSize(new Dimension(300, 500));
prompt.addKeyListener(new KeyMonitor());
JScrollPane js = new JScrollPane(prompt);
js.setViewportView(this.prompt);
js.setVisible(true);
mainFrame.getContentPane().add(js);
mainFrame.pack();
mainFrame.setVisible(true);
this.formattedFields = new FormattedFields().getFField();
init();
}
private class KeyMonitor extends KeyAdapter {
String commandRcvd;
public void keyPressed(KeyEvent e) {
System.out.println("----key pressed: ." + e.getKeyChar() + ".");
String bak = prompt.getText();
System.out.println("\nprevious content: \n\t" + bak);
System.out.println("\nCaret position: " + prompt.getCaretPosition());
// HERE: no matter which key is pressed, the former caret position will be returned. I would like to get there the NEW supposedly caret position.
if(prompt.getCaretPosition() <= lastCmdPosition) {
return;
}
if(e.getKeyCode() == KeyEvent.VK_TAB) {
if(prompt.getText().charAt(prompt.getCaretPosition() -1) == ' ') {
prompt.setText(prompt.getText() + "\n" + printAvailableEntries());
}
else {
System.out.println("." + prompt.getText().charAt(prompt.getCaretPosition() -1 ) + ".");
System.out.println(getCurrentWord());
}
}
if(e.getKeyCode() == KeyEvent.VK_DELETE || e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
if(prompt.getText().charAt(prompt.getCaretPosition() - 2) == '>') {
e.consume();
}
}
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
/*
int lines = prompt.getLineCount();
System.out.println("getLineCount: " + lines);
try {
System.out.println("getLineEndOffset: " + prompt.getLineEndOffset(lines -1));
System.out.println("getLineOfOffset: " + prompt.getLineOfOffset(lines -1));
} catch (BadLocationException ex) {
ex.printStackTrace();
}*/
commandRcvd = prompt.getText().substring(lastCmdPosition);
System.out.println("last command : ." + commandRcvd + ".");
prompt.setText(prompt.getText() + "\n" + PROMPT);
lastCmdPosition = prompt.getCaretPosition();
e.consume();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
if(prompt.getCaretPosition() -1 < lastCmdPosition) {
System.out.println("out : caret=" + prompt.getCaretPosition() + "\tlast=" + lastCmdPosition);
prompt.setCaretPosition(lastCmdPosition);
}
e.consume();
}
if(e.getKeyCode() == KeyEvent.VK_UP) {
prompt.setCaretPosition(prompt.getCaretPosition());
}
System.out.println("\ncurrent content: ." + prompt.getText() + ".");
}
}
public方法命令行(){
JFrame mainFrame=新JFrame();
mainFrame.setLayout(新的FlowLayout(FlowLayout.LEFT));
主机设置位置(200200);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prompt=新的JTextArea(prompt);
prompt.setCaretPosition(prompt.length());
lastCmdPosition=prompt.getCaretPosition();
prompt.setPreferredSize(新维度(300500));
prompt.addKeyListener(新的KeyMonitor());
JScrollPane js=新的JScrollPane(提示);
js.setViewportView(this.prompt);
js.setVisible(true);
mainFrame.getContentPane().add(js);
mainFrame.pack();
mainFrame.setVisible(true);
this.formattedFields=新的formattedFields().getFField();
init();
}
私有类KeyMonitor扩展了KeyAdapter{
字符串命令;
按下公共无效键(按键事件e){
System.out.println(“----键按下:。”+e.getKeyChar()+”);
字符串bak=prompt.getText();
System.out.println(“\n先前的内容:\n\t”+bak);
System.out.println(“\nCret位置:”+prompt.getCaretPosition());
//这里:无论按哪个键,都会返回原来的插入符号位置。我想返回新的假定插入符号位置。
如果(prompt.getCaretPosition()将侦听器方法中调用的代码包装到SwingUtilities.invokeLater()中
将DocumentListener和/或DocumentFilter用于JTextComponents,而不是KeyListenerTop,谢谢!这与java事件Dispatcber线程有关,比如循环中的JProgressBar更新?您询问插入符号位置,但编辑正在进行,插入符号已旧。您只需将检查推迟到编辑结束处理。包装就是这样做的。