JAVA鼠标事件mouseclicked如何区分左右键
请写一个只有单击鼠标右键才会被激发的事件比如右键弹出JPopupMenu只要有启示的关键部分代码给写出每一句的意思...
请写一个只有单击鼠标右键才会被激发的事件比如右键弹出JPopupMenu
只要有启示的关键部分代码
给写出每一句的意思 展开
只要有启示的关键部分代码
给写出每一句的意思 展开
2个回答
展开全部
有关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);
}
}
//<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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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)鼠标右键
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)鼠标右键
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询