JAVA鼠标事件mouseclicked如何区分左右键

请写一个只有单击鼠标右键才会被激发的事件比如右键弹出JPopupMenu只要有启示的关键部分代码给写出每一句的意思... 请写一个只有单击鼠标右键才会被激发的事件比如右键弹出JPopupMenu
只要有启示的关键部分代码
给写出每一句的意思
展开
 我来答
JLU小米
2008-03-29 · TA获得超过544个赞
知道小有建树答主
回答量:305
采纳率:100%
帮助的人:0
展开全部
有关java鼠标和键盘的监听都在这个程序上了!你可以尝试一下~~

//<applet code=TrackEvent width=700 height=500> </applet>

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TrackEvent extends JApplet{
private HashMap h = new HashMap();
private String[] event = {
"focusGained", "focusLost", "keyPressed", "keyReleased",
"keyTyped", "mouseClicked", "mouseEntered", "mouseExited",
"mousePressed", "mouseReleased", "mouseDragged", "mouseMoved",
};
private MyButton b1 = new MyButton(Color.BLUE, "test1");
private MyButton b2 = new MyButton(Color.RED, "test2");

class MyButton extends JButton{
void report(String field, String msg){
((JTextField)h.get(field)).setText(msg);
}
FocusListener f1 = new FocusListener(){
public void focusGained(FocusEvent e){
report("focusGained", e.paramString());
}
public void focusLost(FocusEvent e){
report("focusLost", e.paramString());
}
};

KeyListener k1 = new KeyListener(){
public void keyPressed(KeyEvent e){
report("keyPressed", e.paramString());
}
public void keyReleased(KeyEvent e){
report("keyReleased", e.paramString());
}
public void keyTyped(KeyEvent e){
report("keyTyped",e.paramString());
}
};
MouseListener m1 = new MouseListener(){
public void mouseClicked(MouseEvent e){
report("mouseClicked", e.paramString());
}
public void mouseEntered(MouseEvent e){
report("mouseEntered", e.paramString());
}
public void mouseExited(MouseEvent e){
report("mouseExited", e.paramString());
}
public void mousePressed(MouseEvent e){
report("mousePressed", e.paramString());
}
public void mouseReleased(MouseEvent e){
}

};

MouseMotionListener mm1 = new MouseMotionListener(){
public void mouseDragged(MouseEvent e){
report("mouseDragged", e.paramString());
}
public void mouseMoved(MouseEvent e){
report("mouseMoved" , e.paramString());
}
};

public MyButton(Color color, String label){
super(label);
setBackground(color);
addFocusListener(f1);
addKeyListener(k1);
addMouseListener(m1);
addMouseMotionListener(mm1);
}
}

public void init(){
Container cp = getContentPane();
cp.setLayout(new GridLayout(event.length+1, 2));
for(int i=0; i<event.length; ++i){
JTextField t = new JTextField();
t.setEditable(false);
cp.add(new JLabel(event[i], JLabel.RIGHT));
cp.add(t);
h.put(event[i], t);
}

cp.add(b1);
cp.add(b2);
}

public static void main(String[] args){
Console.run(new TrackEvent(), 700, 500);
}
}

// Tool for running swing demos from the console, both applets and JFrame;

import java.awt.event.*;
import javax.swing.*;

public class Console{
//Creating a title String from the class name
public static String title(Object o){
String t = o.getClass().toString();

if(t.indexOf("class")!= -1)
t = t.substring(6);
return t;
}

public static void run(JFrame frame, int width, int heigth){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,heigth);
frame.setVisible(true);
}

public static void run(JApplet applet, int width, int height){
JFrame frame = new JFrame(title(applet));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width,height);
frame.setLocation(150,250);
applet.init();
applet.start();
frame.setVisible(true);
}
public static void run(JPanel panel, int width, int height){
JFrame frame = new JFrame(title(panel));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.setSize(width,height);
frame.setVisible(true);
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友57ecf53
推荐于2017-12-16
知道答主
回答量:28
采纳率:0%
帮助的人:0
展开全部
public void contentPane_mouseClicked(MouseEvent e) {
this.add(jPopupMenu1);
int mods = e.getModifiers();

//鼠标右键

if ((mods & InputEvent.BUTTON3_MASK) != 0) {

//弹出菜单
jPopupMenu1.show(this, e.getX(), e.getY());

}
}
BUTTON1_MASK)鼠标左键
BUTTON2_MASK)滚周
BUTTON3_MASK)鼠标右键
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式