当前位置: 技术问答>java相关
一个可以接收键盘事件的application修改成applet用IE查看时就无法接收键盘事件,请问应该如何处理?
来源: 互联网 发布时间:2015-01-22
本文导语: application版就是中例8.3的手画程序. 用appletviewer Sketch.html查看时很奇怪,会打开2个一样的窗口(还有一个dos窗口),其中一个窗口可以正常接收键盘事件,而另一个不行. 应该如何改动,才能使内嵌在IE中的applet接收到键盘事...
application版就是中例8.3的手画程序.
用appletviewer Sketch.html查看时很奇怪,会打开2个一样的窗口(还有一个dos窗口),其中一个窗口可以正常接收键盘事件,而另一个不行.
应该如何改动,才能使内嵌在IE中的applet接收到键盘事件?
以下是修改后applet版的代码和相应的Sketch.html网页内容.
//Sketch.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel
implements KeyListener
{ public SketchPanel()
{ addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{ int keyCode = evt.getKeyCode();
int d;
if (evt.isShiftDown())
d = 5;
else
d = 1;
if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
else if (keyCode == KeyEvent.VK_UP) add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
}
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar = evt.getKeyChar();
int d;
if (Character.isUpperCase(keyChar))
{ d = 5;
keyChar = Character.toLowerCase(keyChar);
}
else
d = 1;
if (keyChar == 'h') add(-d, 0);
else if (keyChar == 'l') add(d, 0);
else if (keyChar == 'k') add(0, -d);
else if (keyChar == 'j') add(0, d);
}
public boolean isFocusTraversable() { return true; }
public void add(int dx, int dy)
{ end.x += dx;
end.y += dy;
Graphics g = getGraphics();
g.drawLine(start.x, start.y, end.x, end.y);
g.dispose();
start.x = end.x;
start.y = end.y;
}
private Point start = new Point(50, 50);
private Point end = new Point(50, 50);
}
public class Sketch extends JApplet
{ public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
}
}
网页Sketch.html中的内容:
Sketch
用appletviewer Sketch.html查看时很奇怪,会打开2个一样的窗口(还有一个dos窗口),其中一个窗口可以正常接收键盘事件,而另一个不行.
应该如何改动,才能使内嵌在IE中的applet接收到键盘事件?
以下是修改后applet版的代码和相应的Sketch.html网页内容.
//Sketch.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SketchPanel extends JPanel
implements KeyListener
{ public SketchPanel()
{ addKeyListener(this);
}
public void keyPressed(KeyEvent evt)
{ int keyCode = evt.getKeyCode();
int d;
if (evt.isShiftDown())
d = 5;
else
d = 1;
if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
else if (keyCode == KeyEvent.VK_UP) add(0, -d);
else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
}
public void keyReleased(KeyEvent evt)
{}
public void keyTyped(KeyEvent evt)
{ char keyChar = evt.getKeyChar();
int d;
if (Character.isUpperCase(keyChar))
{ d = 5;
keyChar = Character.toLowerCase(keyChar);
}
else
d = 1;
if (keyChar == 'h') add(-d, 0);
else if (keyChar == 'l') add(d, 0);
else if (keyChar == 'k') add(0, -d);
else if (keyChar == 'j') add(0, d);
}
public boolean isFocusTraversable() { return true; }
public void add(int dx, int dy)
{ end.x += dx;
end.y += dy;
Graphics g = getGraphics();
g.drawLine(start.x, start.y, end.x, end.y);
g.dispose();
start.x = end.x;
start.y = end.y;
}
private Point start = new Point(50, 50);
private Point end = new Point(50, 50);
}
public class Sketch extends JApplet
{ public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
}
}
网页Sketch.html中的内容:
Sketch
|
不好意思,原来的代码在appletviewer下测试的,现在我加了一句,IE中运行好了,你再试试
public class Sketch extends JApplet
{ public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
FocusManager.getCurrentManager().focusNextComponent(contentPane);
}
}
public class Sketch extends JApplet
{ public void init()
{
Container contentPane = getContentPane();
contentPane.add(new SketchPanel());
FocusManager.getCurrentManager().focusNextComponent(contentPane);
}
}