Help me to understand, pls.

  • Follow


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;
}
}
0
Reply yma2037 (32) 2/28/2004 2:34:13 AM

On 27 Feb 2004 18:34:13 -0800, chirs wrote:

> import java.applet.*;
> import java.awt.*;

to detect an event you will most 
likely need to..

import java.awt.event.*;

> 
> public class Echo extends Applet {
> int m;
> Echo st=new Echo();//a

You do not need to instantiate
applets like this, the applet
tag causes the init() method
to be called.


> 	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) {

keydown is deprecated, but since you
did not have it attached to anything,
it makes little difference.

> m=key;
> repaint();
> return true;
> }
> }

You might get some benefit from
looking at a working example that 
detects keyevents, have a look at..
<http://www.physci.org/launcher.jsp#StringTokenizerFrame>

that links to source and a button to 
launch it.  The KeyEvent is actioned
at line 83.

Perhaps you should be consulting the
basic tutorials and JavaDocs a bit more,
and for the moment posting over at 
comp.lang.java.help

-- 
Andrew Thompson
* http://www.PhySci.org/ Open-source software suite
* http://www.PhySci.org/codes/ Web & IT Help
* http://www.1point1C.org/ Science & Technology
0
Reply SeeMySites (3836) 2/28/2004 3:12:49 AM


1 Replies
36 Views

(page loaded in 0.051 seconds)

Similiar Articles:













7/11/2012 5:36:27 AM


Reply: