두 개의 JTextAreas가있는 사용자 인터페이스를 만들었습니다. 하나의 텍스트 영역에서 사용자의 직접 입력을받는 것이 좋겠다. 그러면 나는 본질적으로 다른 것을 정확하게 미러링하는 것을 좋아할 것이다. 그래서 이것은 동일한 문자가 추가 된 것을 의미합니다. 백 스페이스와 키 누르기 등을 정확하게 일치시킵니다. 로봇 (int keycode) 함수를 사용하여 백 스페이스와 리턴 키 누르기를 에뮬레이션하여이를 수행하려고했습니다. 오른쪽 키 코드 10은 Enter이고 8은 백 스페이스입니다. 일반 문자 인 경우 두 번째 JTextArea에 추가하면됩니다. 이 작업은 문자 및 키 누름을 제대로 수행하지만 Backspace를 누르면 알 수없는 문자 사각형이 미러링 텍스트 영역에 그려집니다.자바 스윙 JTextArea 미러링
이상적으로 직접 수동으로 적절한 행동 자신을 운동보다는 OUPUT JTextArea에에 입력 JTextArea에에서 발생하는 이벤트를 통과 할 수 있도록하고 싶습니다. 이것은 keylisten 이벤트를 접수에와 키 코드를 기반으로 수행 할 작업 밖으로 작동 프로그램의 일부가
inputBox = new JTextArea();
inputBoxScroller = new JScrollPane();
inputBoxScroller.setViewportView(inputBox);
inputBox.addKeyListener(this);
입니다 :
이
입력 JTextArea에를 만들고 모든 KeyListener를 할당 프로그램의 일부입니다 키 스트로크 이벤트를 JTextArea 출력으로 '앞으로'전달하는 것이 더 이상적 일 수 있기를 바란다. 당신이 중 하나를 입력 무엇이든 이제JTextArea textArea1 = new JTextArea(...);
JTextArea textArea2 = new JTextArea();
textArea2.setDocument(textArea1.getDocument());
:
public void keyTyped(KeyEvent keyevent) {
String keyPressed = String.valueOf(keyevent.getKeyChar());
int keyCode = keyevent.getKeyCode();
if(keyCode == 10) {
try {
System.out.println(keyevent.toString());
autoKeypresser = new Robot();
outputBox.requestFocus();
autoKeypresser.keyPress(10);
inputBox.requestFocus();
} catch (AWTException e) {
e.printStackTrace();
}
}
if(keyCode == 8) {
try {
autoKeypresser = new Robot();
outputBox.requestFocus();
autoKeypresser.keyPress(8);
inputBox.requestFocus();
} catch (AWTException e) {
e.printStackTrace();
}
} else {
outputBox.append(keyPressed);
}
}
이 작품! 고맙습니다! 그것은 또한 내가 전에했던 일보다 더 빠를 것 같다. 다시 한번 고마워! – tauraamui