java 鼠标监听
java中对鼠标点击监听想整个JAVA小程序中都能够调用怎么写给个例子谢谢分不多不好意思了...
java 中对鼠标点击监听
想整个JAVA小程序中都能够调用怎么写
给个例子 谢谢 分不多 不好意思了 展开
想整个JAVA小程序中都能够调用怎么写
给个例子 谢谢 分不多 不好意思了 展开
2个回答
展开全部
//这是应用程序,跟java小程序的大体思路还是差不多的,你改改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
/**
*建立个界面,可以加载本机中图片。
*加载后可以通过鼠标点击获得图片上任意点坐标。
*/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/**
*main()
*/
public static void main(String[] args){
MyPicture frame = new MyPicture();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
*constructor
*/
public MyPicture(){
setSize(800, 600);//根据要求调整大小
setLocation(100,100);
setTitle("获得图片上任意点坐标");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpic.jpg");//注意图片的路径
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPanel.addMouseListener(this);
con.add(backpicPanel,BorderLayout.CENTER);
tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");
con.add(tipLabel,BorderLayout.NORTH);
}
/**
*
*/
public void mousePressed(MouseEvent e){
int x=e.getX();
int y=e.getY();
String message="("+x+","+y+")";
tipLabel.setText(message);
System.out.println(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/**
*类ImagePanel,用于添加背景图片
*/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcon.getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,0,0,this);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
/**
*建立个界面,可以加载本机中图片。
*加载后可以通过鼠标点击获得图片上任意点坐标。
*/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/**
*main()
*/
public static void main(String[] args){
MyPicture frame = new MyPicture();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
*constructor
*/
public MyPicture(){
setSize(800, 600);//根据要求调整大小
setLocation(100,100);
setTitle("获得图片上任意点坐标");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpic.jpg");//注意图片的路径
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPanel.addMouseListener(this);
con.add(backpicPanel,BorderLayout.CENTER);
tipLabel=new JLabel("--------------------提示:坐标依次打印在屏幕上!--------------------");
con.add(tipLabel,BorderLayout.NORTH);
}
/**
*
*/
public void mousePressed(MouseEvent e){
int x=e.getX();
int y=e.getY();
String message="("+x+","+y+")";
tipLabel.setText(message);
System.out.println(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/**
*类ImagePanel,用于添加背景图片
*/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcon.getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,0,0,this);
}
}
展开全部
这是个显示鼠标坐标的鼠标事件
import java.awt.*;
import java.awt.event.*;
public class TestMouse implements MouseListener,MouseMotionListener{
public static void main(String args[ ]){
TestMouse t=new TestMouse();
t.go();
}
void go(){
Frame f=new Frame("Canvas");
f.setSize(300,300);
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setVisible(true);
}
public void mouseMoved(MouseEvent ev){
int x1=ev.getX();
int y1=ev.getY();
System.out.println("mouse moved"+x1+" "+y1);
}
public void mouseDragged(MouseEvent ev){
System.out.println(ev.paramString());
}
public void mouseClicked(MouseEvent ev){
int x1=ev.getX();
int y1=ev.getY();
int c=ev.getClickCount();
System.out.println("mouse clicked"+x1+" "+y1+" "+c);
}
public void mousePressed(MouseEvent ev){
System.out.println(ev.paramString());
}
public void mouseReleased(MouseEvent ev){
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent ev){
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent ev){
System.out.println("mouseExited");
}
}
import java.awt.*;
import java.awt.event.*;
public class TestMouse implements MouseListener,MouseMotionListener{
public static void main(String args[ ]){
TestMouse t=new TestMouse();
t.go();
}
void go(){
Frame f=new Frame("Canvas");
f.setSize(300,300);
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setVisible(true);
}
public void mouseMoved(MouseEvent ev){
int x1=ev.getX();
int y1=ev.getY();
System.out.println("mouse moved"+x1+" "+y1);
}
public void mouseDragged(MouseEvent ev){
System.out.println(ev.paramString());
}
public void mouseClicked(MouseEvent ev){
int x1=ev.getX();
int y1=ev.getY();
int c=ev.getClickCount();
System.out.println("mouse clicked"+x1+" "+y1+" "+c);
}
public void mousePressed(MouseEvent ev){
System.out.println(ev.paramString());
}
public void mouseReleased(MouseEvent ev){
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent ev){
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent ev){
System.out.println("mouseExited");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询