java 中Windowopened事件问题

chen513902 2010-03-28 06:48:51
import java.awt.*;
import java.awt.event.*;

public class Mouse extends Frame implements MouseListener,MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = 4014615121508512814L;
int x=100,y=100;
int width=400,height=300;//所画矩形的长宽

public Mouse()
{
this.setLayout(null);
this.setSize(800,600);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}
public void windowOpened(WindowEvent e){
drawRectent();
System.out.println("视窗变已开启");
}

});
}


public void drawRectent()
{
Graphics g;
g=getGraphics();
g.setColor(Color.blue);
g.drawRect(x,y,width,height);
System.out.println("视zhengz");
}
public static void main(String[] args) {
Mouse ms=new Mouse();
ms.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {//鼠标单击显示坐标
int x,y;
String xl,yl;
x=e.getX()-this.x;
y=e.getY()-this.y;
if(x>=0&&y>=0&&x<=this.width&&y<=this.height){
if(x>=0)
xl=Integer.toString(x);
else xl="-"+Integer.toString(Math.abs(x));
if(y>=0)
yl=Integer.toString(y);
else yl="-"+Integer.toString(Math.abs(y));
//g=e.getComponent().getGraphics();
System.out.println("xl="+xl+" yl="+yl);
Graphics g;
g=getGraphics();
g.drawString("("+xl+","+yl+")",e.getX(),e.getY());
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseMoved(MouseEvent e) {//鼠标移动显示坐标
int x,y;
String xl,yl;
x=e.getX()-this.x;
y=e.getY()-this.y;
if(x>=0&&y>=0&&x<=this.width&&y<=this.height){
System.out.println("x="+x+" y="+y);
if(x>=0)
xl=Integer.toString(x);
else xl="-"+Integer.toString(Math.abs(x));
if(y>=0)
yl=Integer.toString(y);
else yl="-"+Integer.toString(Math.abs(y));
Graphics g;
g=getGraphics();
g.drawString("("+xl+","+yl+")",e.getX(),e.getY());
repaint();
}
}
}

红色部分怎么没执行?
...全文
243 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wd9053 2010-04-05
  • 打赏
  • 举报
回复
顶楼上,setVisible()顺序问题
import java.awt.*;
import java.awt.event.*;

public class Mouse extends Frame implements MouseListener,MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = 4014615121508512814L;
int x=100,y=100;
int width=400,height=300;//所画矩形的长宽

public Mouse()
{
this.setLayout(null);
this.setSize(800,600);
this.addMouseListener(this);
this.addMouseMotionListener(this);
//this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}
public void windowOpened(WindowEvent e){
drawRectent();
System.out.println("视窗变已开启");
}
});

this.setVisible(true);////////////////////
}


public void drawRectent()
{
Graphics g;
g=getGraphics();
g.setColor(Color.blue);
g.drawRect(x,y,width,height);
System.out.println("视zhengz");
}
public static void main(String[] args) {
Mouse ms=new Mouse();
ms.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {//鼠标单击显示坐标
int x,y;
String xl,yl;
x=e.getX()-this.x;
y=e.getY()-this.y;
if(x>=0&&y>=0&&x<=this.width&&y<=this.height){
if(x>=0)
xl=Integer.toString(x);
else xl="-"+Integer.toString(Math.abs(x));
if(y>=0)
yl=Integer.toString(y);
else yl="-"+Integer.toString(Math.abs(y));
//g=e.getComponent().getGraphics();
System.out.println("xl="+xl+" yl="+yl);
Graphics g;
g=getGraphics();
g.drawString("("+xl+","+yl+")",e.getX(),e.getY());
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseMoved(MouseEvent e) {//鼠标移动显示坐标
int x,y;
String xl,yl;
x=e.getX()-this.x;
y=e.getY()-this.y;
if(x>=0&&y>=0&&x<=this.width&&y<=this.height){
System.out.println("x="+x+" y="+y);
if(x>=0)
xl=Integer.toString(x);
else xl="-"+Integer.toString(Math.abs(x));
if(y>=0)
yl=Integer.toString(y);
else yl="-"+Integer.toString(Math.abs(y));
Graphics g;
g=getGraphics();
g.drawString("("+xl+","+yl+")",e.getX(),e.getY());
repaint();
}
}
}
chen513902 2010-04-05
  • 打赏
  • 举报
回复
你试过没有,好像还是不行
focusforce 2010-03-29
  • 打赏
  • 举报
回复
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

@Override
public void windowOpened(WindowEvent e)
{
drawRectent();
System.out.println("视窗变已开启");
}

});
this.setVisible(true);
这么写就好了,先添加监听器再设置visible。
6.2.4 事件适配器 适配器类实现一个对应的所有接口,只是方法为空。 public abstract class WindowAdapter implements WindowListener { public void windowOpened(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} } 表6.1 Listener接口与对应的适配器(Adapter)类 接口名称 适配器名称 ComponentListener ComponentAdapter ContainerListener ContainerAdapter FocusListener FocusAdapter KeyListener KeyAdapter MouseListener MouseAdapter MouseMotionListener MouseMotionAdapter WindowListener WindowAdapter (7)鼠标运动事件 鼠标运动事件发生在鼠标移过某个组件时。 任何组件都可以产生这些事件,通过接口 MouseMotionListener 实现。 该接口有两个方法: mouseDragged(MouseEvent) mouseMove(MouseEvent) MouseMotionListener 没有自己的事件类型,替代它的是 MouseEvent 。 被用在 MouseEvent 对象上的方法它都可以使用: getClickCount( ) 返回鼠标被单击次数(整数) getPoint( ) 返回鼠标被单击位置的 x ,y 坐标 getX( ) 返回 x 位置 getY( ) 返回 y 位置 (8)窗口事件WindowEvent) 窗口事件发生在用户打开或关闭一个诸如 Frame 或 Window 的窗口时。任何组件都可以产生这些事件,为了支持它们,类必须实现接口:WindowListener。 接口 WindowListener 有七个方法: windowActivated(WindowEvent) 窗口被激活 windowClosed (WindowEvent) 窗口已关闭 windowClosing (WindowEvent) 窗口正在关闭 windowDeactivated (WindowEvent) 窗口失效 windowDeiconified (WindowEvent) 窗口正常化时 windowIconified (WindowEvent) 窗口最小化时 windowOpened (WindowEvent) 窗口被打开 (5)键盘事件(KeyEvent) 发生在键盘上的某个键被按下时。 类为了能够处理这些事件必须实现接口 KeyListener 。 每个产生一个键盘事件的组件上要调用方法 addKeyListener( ) 在接口 KeyListener 有三个方法: public void keyPressed(KeyEvent evt) { //…… } public void keyReleased(KeyEvent evt) { //…… } public void keyTyped(KeyEvent evt) { //…… } 在 KeyEvent 对象上可以使用的方法: getKeyChar( ) 返回与事件相关的键盘字符的 Unicode 码 . . . . . . . . . . . . . .

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧