0
KeyListener interface
을 사용하여 내 프레임에 문자열을 그리려면이 코드를 시도했지만 키보드의 입력 가능한 키를 누를 때마다 프레임에 나타나야하지만 오류가 없더라도 작동하지 않습니다.프레임에 문자열이없는 이유는 무엇입니까?
누군가가 실수를 말할 수 있습니까?
다음은 내 코드입니다 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class KeyevntFrame2 extends Frame {
Button b = new Button("ok");
Button b1 = new Button("hey");
char ch;
String s = "";
public KeyevntFrame2() {
setTitle("understand key events");
setSize(800, 600);
addKeyListener(new KeyHandler());
setFont(new Font("Arial", Font.PLAIN, 35));
setForeground(Color.BLACK);
add(b);
add(b1);
b.setBounds(200, 200, 100, 100);
b1.setBounds(200, 700, 100, 100);
setLayout(null);
b.addActionListener(new KeyHandler());
b1.addActionListener(new KeyHandler());
}
class KeyHandler implements KeyListener, ActionListener {
public void keyPressed(KeyEvent e) {
ch = e.getKeyChar();
s = s + ch;
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g) {
g.drawString(s, 300, 200);
g.setFont(new Font("Arial", Font.PLAIN, 35));
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(b1, "thank you for using java");
}
}
public static void main(String a[]) {
KeyevntFrame2 f = new KeyevntFrame2();
f.setVisible(true);
}
}
처리기 메서드가 실제로 적중되는지 여부를 확인하기 위해 중단 점을 배치 했습니까? – LordWilmore
[누군가 내 질문에 답변 할 때 어떻게해야합니까?] (https://stackoverflow.com/help/someone-answers)를 읽어보십시오. –