JApplet 不接收鼠标事件
JApplet does not receive mouse-events
我正在尝试在 JFrame 中的 JApplet 上收听 Java 中的 MouseEvents。我的程序实际上做了更多的事情,但我已经为您将其简化为:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class mouse extends JApplet implements MouseListener, MouseMotionListener {
static final private int SCREENW = 800;
static final private int SCREENH = 300;
private static JFrame window = null;
public mouse() {
if(window!=null)
return;
window = new JFrame("Window");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
window.addMouseListener( this );
window.addMouseMotionListener( this );
window.getContentPane().add("Center", this);
window.pack();
window.setSize(new Dimension(SCREENW, SCREENH));
window.setVisible(true);
window.setFocusable(true);
window.requestFocusInWindow();
}
public void mouseEntered( MouseEvent e ) {
System.out.println("Test0");
}
public void mouseExited( MouseEvent e ) {
System.out.println("Test1");
}
public void mouseClicked( MouseEvent e ) {
System.out.println("Test2");
}
public void mousePressed( MouseEvent e ) {
System.out.println("Test3");
}
public void mouseReleased( MouseEvent e ) {
System.out.println("Test4");
}
public void mouseMoved( MouseEvent e ) {
System.out.println("Test5");
}
public void mouseDragged( MouseEvent e ) {
System.out.println("Test6");
}
public static void main(String s[]) {
mouse Mouse = new mouse();
}
}
注意:我正在 Ubuntu 14.04 机器上测试所有这些。当 运行 'java mouse' 时,window 打开但没有事件被触发。怎么了?
您知道 JApplets 可以自己绘制吗?如果在网页中 运行,从扩展 JApplet 的 class 创建框架将导致页面中出现 JApplet 运行,而 JFrame 会弹出。
You can see an example here,基本上您不需要 JFrame 来显示您的原语。一个 paint() 函数应该足以做到这一点。
window.setBounds(100, 100, SCREENW, SCREENH);
失踪了!
我正在尝试在 JFrame 中的 JApplet 上收听 Java 中的 MouseEvents。我的程序实际上做了更多的事情,但我已经为您将其简化为:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class mouse extends JApplet implements MouseListener, MouseMotionListener {
static final private int SCREENW = 800;
static final private int SCREENH = 300;
private static JFrame window = null;
public mouse() {
if(window!=null)
return;
window = new JFrame("Window");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
window.addMouseListener( this );
window.addMouseMotionListener( this );
window.getContentPane().add("Center", this);
window.pack();
window.setSize(new Dimension(SCREENW, SCREENH));
window.setVisible(true);
window.setFocusable(true);
window.requestFocusInWindow();
}
public void mouseEntered( MouseEvent e ) {
System.out.println("Test0");
}
public void mouseExited( MouseEvent e ) {
System.out.println("Test1");
}
public void mouseClicked( MouseEvent e ) {
System.out.println("Test2");
}
public void mousePressed( MouseEvent e ) {
System.out.println("Test3");
}
public void mouseReleased( MouseEvent e ) {
System.out.println("Test4");
}
public void mouseMoved( MouseEvent e ) {
System.out.println("Test5");
}
public void mouseDragged( MouseEvent e ) {
System.out.println("Test6");
}
public static void main(String s[]) {
mouse Mouse = new mouse();
}
}
注意:我正在 Ubuntu 14.04 机器上测试所有这些。当 运行 'java mouse' 时,window 打开但没有事件被触发。怎么了?
您知道 JApplets 可以自己绘制吗?如果在网页中 运行,从扩展 JApplet 的 class 创建框架将导致页面中出现 JApplet 运行,而 JFrame 会弹出。
You can see an example here,基本上您不需要 JFrame 来显示您的原语。一个 paint() 函数应该足以做到这一点。
window.setBounds(100, 100, SCREENW, SCREENH);
失踪了!