답변 감사합니다. 그리고요.


[ 다음 글들 ] [ 이어서 글올리기(답하기) ] [ 자바 묻고 답하기 ]

글쓴이 :odero 2000년 3월 23일 16:35:06

In Reply to: FocusManager를 사용해보세요. posted by 윤경구 on 2000년 3월 22일 19:13:09:

FocusManager를 생각 안했던 것은 아니라 뒤적뒤적 거리면서 했는데.. 생각보다 매니져를 쓰기가 제겐 좀 어렵더라구요.
이것저것 해보다가..keyevent와 requestFocus()로 원하는 것은 해결했습니다.
아래는 소스입니다.
/*
* Swing version
*/


import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;


public class KeyEventDemo extends JApplet implements KeyListener,ActionListener {
JTextField typingArea2;
JTextField typingArea1;
JTextArea displayArea;
static final String newline = "\n";


public void init() {

JButton button = new JButton("Clear");
button.addActionListener(this);


typingArea1 = new JTextField(10);
typingArea1.addKeyListener(this);


typingArea2 = new JTextField(10);
typingArea2.addKeyListener(this);
displayArea = new JTextArea();
displayArea.setEditable(true);

JPanel contentPane = new JPanel();
JPanel content1 = new JPanel();
contentPane.setLayout(new BorderLayout());

content1.add(typingArea1);
content1.add(typingArea2);
content1.add(button);
contentPane.add(content1, BorderLayout.NORTH);
contentPane.add(displayArea, BorderLayout.CENTER);
typingArea1.requestFocus();
setContentPane(contentPane);
}



/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
}


/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
String text1 = typingArea1.getText();
String text2 = typingArea2.getText();

if(text1.length() == 4){
text1 = text1 + "/";
} else if ( text1.length() == 7 ) {
text1 = text1 + "/";
} else if(text1.length() ==10){
typingArea2.requestFocus();
}
if(text2.length() == 4){
text2 = text2 + "/";
}else if(text2.length()==7){
text2 = text2 + "/";
}else if(text2.length()==10){
displayArea.requestFocus();
displayInfo(e,text1,text2);
}
typingArea1.setText(text1);
typingArea2.setText(text2);
}


/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {

}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
typingArea1.setText("");
typingArea2.setText("");
displayArea.setText("");


//Return the focus to the typing area.
typingArea1.requestFocus();
}


/*
* We have to jump through some hoops to avoid
* trying to print non-printing characters
* such as Shift. (Not only do they not print,
* but if you put them in a String, the characters
* afterward won't show up in the text area.)
*/
protected void displayInfo(KeyEvent e, String s1,String s2){
String charString, keyCodeString, modString, tmpString;


char c = e.getKeyChar();
int keyCode = e.getKeyCode();
int modifiers = e.getModifiers();


if (Character.isISOControl(c)) {
charString = "key character = "
+ "(an unprintable control character)";
} else {
charString = "key character = '"
+ c + "'";
}


keyCodeString = "key code = " + keyCode
+ " ("
+ KeyEvent.getKeyText(keyCode)
+ ")";


modString = "modifiers = " + modifiers;
tmpString = KeyEvent.getKeyModifiersText(modifiers);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no modifiers)";
}


displayArea.append("시작일:" +s1 + " " + "종료일:" +s2+ newline
+ " " + charString + newline
+ " " + keyCodeString + newline
+ " " + modString + newline);
}
}






다음 글들:



이어서 글올리기(답하기)

이름:
E-Mail:
제목:
내용:
HTML 태그 포함 여부: HTML 문서일 경우 체크
관련 URL(선택):
URL 제목(선택):
관련 이미지 URL:


[ 다음 글들 ] [ 이어서 글올리기(답하기) ] [ 자바 묻고 답하기 ]