我正在尝试在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'时,窗口会打开,但不会触发任何事件。有什么问题?
答案 0 :(得分:0)
您是否意识到JApplets可以自己绘制?如果在网页中运行,从扩展JApplet的类创建框架将导致JApplet在页面中运行,而JFrame将弹出。
You can see an example here,基本上你不需要JFrame来显示基元。 paint()函数应该足以做到这一点。
答案 1 :(得分:0)
window.setBounds(100, 100, SCREENW, SCREENH);
失踪了!