社区
Java SE
帖子详情
关于MouseMotionListener
kingofvc
2003-10-19 03:20:16
在mouseDragged的时候如何判断是鼠标左键还是右键?
getButton判断的结果不对
...全文
172
11
打赏
收藏
关于MouseMotionListener
在mouseDragged的时候如何判断是鼠标左键还是右键? getButton判断的结果不对
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
liad
2003-10-20
打赏
举报
回复
为什么不贴出正确的答案呢?我刚才又去搜索一下,通过下列方法可以获得
public void mouseDragged(MouseEvent event) {
String button = "";
if ((event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0)
button = "Left Button";
else if (((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0)
button = "Right Button";
不知你所说的直接的方法是不是这个?
kingofvc
2003-10-20
打赏
举报
回复
搞定了 果然还有直接的方法 sun没让人失望
kingofvc
2003-10-20
打赏
举报
回复
再浮出来看看 我不信没有直接的方法
kingofvc
2003-10-20
打赏
举报
回复
救命……
yao_xh
2003-10-20
打赏
举报
回复
先按照 liad() 的方法来。
kingofvc
2003-10-20
打赏
举报
回复
up
kingofvc
2003-10-19
打赏
举报
回复
这是我最不愿意用的方法 虽然也是最先想到的 感觉就是用全局变量!
我想应该有这么一个方法可以直接得到吧
liad
2003-10-19
打赏
举报
回复
在public void mouseDragged(MouseEvent event) {
直接得不到
可以定义一个成员变量
int lastMousePressed;
来保存最后点击的键
然后
public void mousePressed(MouseEvent event) {
this.lastMousePressed = event.getButton();
......
public void mouseReleased(MouseEvent event) {
this.lastMousePressed = -1;
最后
public void mouseDragged(MouseEvent event) {
String button = "";
if (this.lastMousePressed == MouseEvent.BUTTON1)
button = "Left Button";
else if (this.lastMousePressed == MouseEvent.BUTTON3)
right = "Right Button";
kingofvc
2003-10-19
打赏
举报
回复
up
kingofvc
2003-10-19
打赏
举报
回复
help!
kingofvc
2003-10-19
打赏
举报
回复
help!
java 使用
Mouse
Mot
ion
Listener
监听鼠标
java 使用
Mouse
Mot
ion
Listener
监听鼠标 java 使用
Mouse
Mot
ion
Listener
监听鼠标
java多态作业
java多态import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Complex
Listener
implements
Mouse
Mot
ion
Listener
,
Mouse
Listener
,Act
ion
Listener
{ JFrame f; JTextArea tf; JButton bt; int number = 1; public Complex
Listener
(){ JLabel label =new JLabel("click and Drag the
mouse
"); f=new JFrame ("Complex
Listener
"); tf=new JTestArea(); bt= new JButton("退出"); tf.add
Mouse
Mot
ion
Listener
(this); tf.add
Mouse
Listener
(this); bt.addAct
ion
Listener
(this); f.add(label,BorderLayout.NORTH); f.add(tf,BorderLayout.CENTER); f.add(bt,BorderLayout.SOUTH); f.setSize(300,200); f.setVisible(true); //
Mouse
Mot
ion
Listener
的方法 public void
mouse
Dragged(
Mouse
Event e){ String s=number+++""+"The
mouse
Dragged:x="+ e.getX()+"y="+e.getY()+"\n"; tf.append(s); } //
Mouse
Mot
ion
Listener
的方法 public void
mouse
Entered(
Mouse
Event e){ String s=number+++""+"The
mouse
entered"+"\n"; tf.append(s); } public void
mouse
Clicked(
Mouse
Event e){ String s=number+++""+"The
mouse
clicked."+"\n"; tf.append(s); } public void
mouse
Exited(
Mouse
Event e){ String s=number+++""+"The
mouse
exit."+"\n"; tf.append(s); } //未使用的
Mouse
Mot
ion
Listener
的方法 public void
mouse
Moved(
Mouse
Event e){ } //未使用的
Mouse
Listener
的方法 public void
mouse
Released(
Mouse
Event e){ } public void
mouse
Pressed(
Mouse
Event e){ } //Act
ion
Listener
的方法 public void act
ion
Performed(Act
ion
Event e){ System.exit(0); public static void main(String args[]){ Complex
Listener
two = new Complex
Listener
(); } }
飞机大战源代码
简单的飞机大战游戏import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.
Mouse
Event; import java.awt.event.
Mouse
Mot
ion
Listener
; import java.util.ArrayList; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JPanel; public class MyJPanel extends JPanel implements Runnable,
Mouse
Mot
ion
Listener
{ Yingx
ion
gji yingx
ion
g = new Yingx
ion
gji(); ArrayList
zidans = new ArrayList
(); boolean isFire = false;//是否冒泡(发射) boolean pass=false; ArrayList
dijis = new ArrayList
();//保存所有的敌机 int dicount = 15; Image yingx
ion
gImg =null; Image zidanImg = null; Image dijiImgs[] = new Image[15]; Image beijingImg = null; int beijingY = -720; Random ran = new Random();
java画图程序画布类
主要是画图程序的一个类import java.awt.*; import java.awt.event.
Mouse
Event; import java.awt.event.
Mouse
Listener
; import java.awt.event.
Mouse
Mot
ion
Listener
; import java.util.ArrayList; import javax.swing.*; /** * @author Administrator * */ public class HuaBu extends JPanel implements
Mouse
Mot
ion
Listener
,
Mouse
Listener
{ private ArrayList
_arrayshape; private ShapeData _shapeData; public ShapeData getShapeData(){ return this._shapeData; } public void setShapaData(ShapeData shape){ this._shapeData=shape; } public HuaBu(){ this._shapeData=new ShapeData(); this._arrayshape=new ArrayList
(); //this._arrayshape.add(new ShapeData()); this.setBackground(Color.WHITE); this.add
Mouse
Listener
(this); this.add
Mouse
Mot
ion
Listener
(this); } public void paint(Graphics oldPainter){ super.paint(oldPainter); Graphics2D painter=(Graphics2D)oldPainter; painter.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
java图形界面开发-事件监听
6.2.4 事件适配器 适配器类实现一个对应的所有接口,只是方法为空。 public abstract class WindowAdapter implements Window
Listener
{ 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)类 接口名称 适配器名称 Component
Listener
ComponentAdapter Container
Listener
ContainerAdapter Focus
Listener
FocusAdapter Key
Listener
KeyAdapter
Mouse
Listener
Mouse
Adapter
Mouse
Mot
ion
Listener
Mouse
Mot
ion
Adapter Window
Listener
WindowAdapter (7)鼠标运动事件 鼠标运动事件发生在鼠标移过某个组件时。 任何组件都可以产生这些事件,通过接口
Mouse
Mot
ion
Listener
实现。 该接口有两个方法:
mouse
Dragged(
Mouse
Event)
mouse
Move(
Mouse
Event)
Mouse
Mot
ion
Listener
没有自己的事件类型,替代它的是
Mouse
Event 。 被用在
Mouse
Event 对象上的方法它都可以使用: getClickCount( ) 返回鼠标被单击次数(整数) getPoint( ) 返回鼠标被单击位置的 x ,y 坐标 getX( ) 返回 x 位置 getY( ) 返回 y 位置 (8)窗口事件(WindowEvent) 窗口事件发生在用户打开或关闭一个诸如 Frame 或 Window 的窗口时。任何组件都可以产生这些事件,为了支持它们,类必须实现接口:Window
Listener
。 接口 Window
Listener
中有七个方法: windowActivated(WindowEvent) 窗口被激活 windowClosed (WindowEvent) 窗口已关闭 windowClosing (WindowEvent) 窗口正在关闭 windowDeactivated (WindowEvent) 窗口失效 windowDeiconified (WindowEvent) 窗口正常化时 windowIconified (WindowEvent) 窗口最小化时 windowOpened (WindowEvent) 窗口被打开 (5)键盘事件(KeyEvent) 发生在键盘上的某个键被按下时。 类为了能够处理这些事件必须实现接口 Key
Listener
。 每个产生一个键盘事件的组件上要调用方法 addKey
Listener
( ) 在接口 Key
Listener
中有三个方法: public void keyPressed(KeyEvent evt) { //…… } public void keyReleased(KeyEvent evt) { //…… } public void keyTyped(KeyEvent evt) { //…… } 在 KeyEvent 对象上可以使用的方法: getKeyChar( ) 返回与事件相关的键盘字符的 Unicode 码 . . . . . . . . . . . . . .
Java SE
62,614
社区成员
307,327
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章