AWT 框架不处理事件
AWT Frame does not handle events
框架打开和关闭正常,但鼠标点击不起作用
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Create a frame window that responds to mouse click
public class AWT3 extends Frame {
String Mmsg="";
int mouseX=0, mouseY=0;
public AWT3() {
addWindowListener(new MyWindowwAdapter(this));
addMouseListener(new MyMouseeAdapter(this));
}
public void paint(Graphics g){
g.drawString(Mmsg, mouseX, mouseY);
}
public static void main(String args[]){
AWT3 awt3 = new AWT3();
awt3.setSize(new dimension(500, 500));
awt3.setTitle("Window framee");
awt3.setVisible(true);
}
}
class MyWindowwAdapter extends WindowAdapter{
AWT3 awt3;
public MyWindowwAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void windowClosing(WindowEvent we){
awt3.setVisible(false);
}
}
class MyMouseeAdapter extends MouseAdapter{
AWT3 awt3;
public MyMouseeAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void MouseClicked(MouseEvent me){
awt3.Mmsg="the mouse is clicked";
awt3.mouseX= me.getX();
awt3.mouseY=me.getY();``
awt3.repaint();
}
}
方法名应该是public void mouseClicked(MouseEvent me)
而不是 public void MouseClicked(MouseEvent me)
.
mouseClicked() 是按下并释放鼠标按钮的时间。
mousePressed() 是当鼠标按钮被按下时。
您的代码正在运行。在 java 1.7 上测试。只有我看到的问题是没有导入 java.awt.Dimension class 您正在尝试创建一个新维度 (500, 500);虽然 class 名称是简单形式,但您可以修复此错误并尝试代码。
从表面上看,这段代码无法编译。您有需要修复的错误:
awt3.setSize(new dimension(500, 500));
到
awt3.setSize(new Dimension(500, 500));
并添加其他人指出的正确 import java.awt.Dimension
。
另一个错误是 MouseClicked(MouseEvent me)
没有覆盖 MouseAdapter
中的超级 class 方法,因为它在语法上是错误的(超级 class 方法以小写开头)。将其更改为 mouseClicked(MouseEvent me)
(如果需要,可以添加可选的 @Override
注释)。
框架打开和关闭正常,但鼠标点击不起作用
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Create a frame window that responds to mouse click
public class AWT3 extends Frame {
String Mmsg="";
int mouseX=0, mouseY=0;
public AWT3() {
addWindowListener(new MyWindowwAdapter(this));
addMouseListener(new MyMouseeAdapter(this));
}
public void paint(Graphics g){
g.drawString(Mmsg, mouseX, mouseY);
}
public static void main(String args[]){
AWT3 awt3 = new AWT3();
awt3.setSize(new dimension(500, 500));
awt3.setTitle("Window framee");
awt3.setVisible(true);
}
}
class MyWindowwAdapter extends WindowAdapter{
AWT3 awt3;
public MyWindowwAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void windowClosing(WindowEvent we){
awt3.setVisible(false);
}
}
class MyMouseeAdapter extends MouseAdapter{
AWT3 awt3;
public MyMouseeAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void MouseClicked(MouseEvent me){
awt3.Mmsg="the mouse is clicked";
awt3.mouseX= me.getX();
awt3.mouseY=me.getY();``
awt3.repaint();
}
}
方法名应该是public void mouseClicked(MouseEvent me)
而不是 public void MouseClicked(MouseEvent me)
.
mouseClicked() 是按下并释放鼠标按钮的时间。
mousePressed() 是当鼠标按钮被按下时。
您的代码正在运行。在 java 1.7 上测试。只有我看到的问题是没有导入 java.awt.Dimension class 您正在尝试创建一个新维度 (500, 500);虽然 class 名称是简单形式,但您可以修复此错误并尝试代码。
从表面上看,这段代码无法编译。您有需要修复的错误:
awt3.setSize(new dimension(500, 500));
到
awt3.setSize(new Dimension(500, 500));
并添加其他人指出的正确 import java.awt.Dimension
。
另一个错误是 MouseClicked(MouseEvent me)
没有覆盖 MouseAdapter
中的超级 class 方法,因为它在语法上是错误的(超级 class 方法以小写开头)。将其更改为 mouseClicked(MouseEvent me)
(如果需要,可以添加可选的 @Override
注释)。