import java.awt.*;
import java.awt.event.*;public class TestAWT extends MouseAdapter
{
private Frame frame;
private TextArea textArea;
private MenuItem about;
private MenuItem exit;
private PopupMenu popupMenu;
private Panel panel;
public void initGUI()
{
frame=new Frame("mouse event");
textArea=new TextArea("",10,10);
textArea.addMouseListener(this);
frame.add(textArea);
frame.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON1_MASK){
textArea.append("点击左键"+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n");
}
else if(e.getModifiers()==e.BUTTON3_MASK)
{
textArea.append("点击右键:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n");
}
}
public static void main (String[] args)
{
TestAWT example=new TestAWT();
example.initGUI();
}
}请人帮忙一下、、这代码解释一下、、 看了一星期的GUI感觉还是什么都不会、、 有人可以谈谈学习GUI的经验吗、、特别是事件处理那块、一点也不理解、、、 感谢万分、
import java.awt.event.*;public class TestAWT extends MouseAdapter
{
private Frame frame;
private TextArea textArea;
private MenuItem about;
private MenuItem exit;
private PopupMenu popupMenu;
private Panel panel;
public void initGUI()
{
frame=new Frame("mouse event");
textArea=new TextArea("",10,10);
textArea.addMouseListener(this);
frame.add(textArea);
frame.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON1_MASK){
textArea.append("点击左键"+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n");
}
else if(e.getModifiers()==e.BUTTON3_MASK)
{
textArea.append("点击右键:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n");
}
}
public static void main (String[] args)
{
TestAWT example=new TestAWT();
example.initGUI();
}
}请人帮忙一下、、这代码解释一下、、 看了一星期的GUI感觉还是什么都不会、、 有人可以谈谈学习GUI的经验吗、、特别是事件处理那块、一点也不理解、、、 感谢万分、
解决方案 »
- 两个整数相除,结果保留一位小数点,怎么做。。。
- java 如何 实现 动态 读取配置文件,请多指教,谢谢!!
- 我今天装的J2SE1.4.2 SDK,第一个程序,有问题
- 初学者请教JAVA一个问题:我想在菜单点击事件中打开本地存放的.CHM文件,应该如何做?
- 500分,关于jboss集群及性能测试,有集群经验的请进!
- 这个题我认为应该选E,对吗?
- 离开csdn一段时间,静下心来好好想一想。散分!!
- jar文件能不能编译为.exe文件!!
- 为什么报空指针错误呢
- 大家好:org.springframework.beans.factory.NoUniqueBeanDefinitionException
- 有趣的问题,看哪位大虾能帮忙解决下
- 一道题,我想了几天也没想出来,有兴趣的来试试
对testArea注册监听、、当在textArea区域点击鼠标时产生一个MouseEvent事件、、执行
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON1_MASK){
//判断点击的是不是左键
textArea.append("点击左键"+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n"); //输出点击时鼠标的X坐标和Y坐标
textArea.append("点击次数"+e.getClickCount()+"\r\n");
} else if(e.getModifiers()==e.BUTTON3_MASK) //判断点击的是不是右键。。
{
textArea.append("点击右键:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n"); }
} 记得给分哟、、(*^__^*) 嘻嘻……
import java.awt.event.*; public class TestAWT extends MouseAdapter //MouseAdapter 接收鼠标事件的抽象适配器类。
{
private Frame frame;
private TextArea textArea;
//private MenuItem about;
//private MenuItem exit;
//private PopupMenu popupMenu;
//private Panel panel;
/*
* 这些程序中没有用到啊
*/public void initGUI()
{
frame=new Frame("mouse event");
textArea=new TextArea("",10,10); //构造一个新文本区,该文本区具有指定的文本,以及指定的行数和列数。
textArea.addMouseListener(this); //为文本域textArea添加事件
frame.add(textArea);
frame.setSize(200,200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}); //关闭按钮的关闭命令
} public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==e.BUTTON1_MASK){
textArea.append("点击左键"+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n");
}
/*
* e.getModifiers()==e.BUTTON1_MASK:当鼠标点击左键时,发生
* XXXX.append(String)将给定文本追加到文本区的当前文本
*/else if(e.getModifiers()==e.BUTTON3_MASK)
{
textArea.append("点击右键:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n"); }
/*
* e.getModifiers()==e.BUTTON3_MASK:当鼠标点击右键时,发生
*/}
public static void main (String[] args)
{
TestAWT example=new TestAWT();
example.initGUI();
}
}
/n的意思是 “回车”
[b]加油!!![/b]
public void mousePressed(MouseEvent e) 监听事件处理
e.getModifiers()==e.BUTTON3_MASK 适配事件
frame.addWindowListener(new WindowAdapter(){ //关闭窗口
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
} public void mousePressed(MouseEvent e) //重写MouseAdapter中的mousePressed方法
{
if(e.getModifiers()==e.BUTTON1_MASK){//如果是左键单击,记住e.BUTTON1_MASK是左键就行
textArea.append("点击左键"+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n"); //通过单击事件去获得单击位置的坐标
textArea.append("点击次数"+e.getClickCount()+"\r\n");
} else if(e.getModifiers()==e.BUTTON3_MASK) //如果是右键单击
{
textArea.append("点击右键:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击位置:x="+e.getX()+" y="+e.getY()+"\r\n");
textArea.append("点击次数"+e.getClickCount()+"\r\n"); }
}
public static void main (String[] args)
{
TestAWT example=new TestAWT();
example.initGUI();
}
}
因为你继承了MouseAdapter ,所以这个类本身就是一个监听器,你只要重写方法,不用手工注册监听了
因为这里有一些接口什么的,还有一些父类引用指向子类对象!其实这一块不难,楼主加油啊!
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
这里也是这样的,没个###Listener都有对应的###Adapter
这里还用到了匿名类