C
chirs
Hi,
Here is the applet, which displays a red x. It should display a
letter and a number when I type a key. The problem is line //a and
//b. Why do they cause problem? Why does it show a red x?
If I do not use these two lines and use a line
this.requestFocus();
it does not work either. But why? Since line //c is ok. What's wrong
with "this."? Line //c does not cause any problem. But I still need
to click the applet to get the focus. It seems there is no way to set
focus thru code on the applet.
Below is the code. Thanks a lot - Chris
import java.applet.*;
import java.awt.*;
public class Echo extends Applet {
int m;
Echo st=new Echo();//a
public void init() {
//requestFocus();//c
st.requestFocus();//b
setBackground(Color.pink);
}
public void paint(Graphics g) {
if (!(m==0)){
g.drawString(Integer.toString(m),100,100);
g.drawString("" + (char)m,200,200);
g.drawString("test" + 1,200,300);}
}
public boolean keyDown(Event evt, int key) {
m=key;
repaint();
return true;
}
}
Here is the applet, which displays a red x. It should display a
letter and a number when I type a key. The problem is line //a and
//b. Why do they cause problem? Why does it show a red x?
If I do not use these two lines and use a line
this.requestFocus();
it does not work either. But why? Since line //c is ok. What's wrong
with "this."? Line //c does not cause any problem. But I still need
to click the applet to get the focus. It seems there is no way to set
focus thru code on the applet.
Below is the code. Thanks a lot - Chris
import java.applet.*;
import java.awt.*;
public class Echo extends Applet {
int m;
Echo st=new Echo();//a
public void init() {
//requestFocus();//c
st.requestFocus();//b
setBackground(Color.pink);
}
public void paint(Graphics g) {
if (!(m==0)){
g.drawString(Integer.toString(m),100,100);
g.drawString("" + (char)m,200,200);
g.drawString("test" + 1,200,300);}
}
public boolean keyDown(Event evt, int key) {
m=key;
repaint();
return true;
}
}