java监听鼠标代码 java监听鼠标位置
java中鼠标监听器的使用
import java.util.Date;
为嘉祥等地区用户提供了全套网页设计制作服务,及嘉祥网站建设行业解决方案。主营业务为网站制作、网站设计、嘉祥网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame {//删除了集成的接口
JFrame frame = new JFrame();
JLabel label = null;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton btn1 = new JButton(" 红 色 ");
JButton btn2 = new JButton(" 绿 色 ");
JButton btn3 = new JButton(" 蓝 色 ");
public MyTime() {
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);// 创建一个监听器对象
btn1.addActionListener(new TimeThread(this).new AL()); // 为按钮注册监听器(使用了TimeThread类的AL内部类)
btn2.addActionListener(new TimeThread(this).new AL());
btn3.addActionListener(new TimeThread(this).new AL());
frame = new JFrame();
label = new JLabel();
label.setIcon(new ImageIcon("QQ拼音截图未命名.jpg"));
label.setFont(new Font("幼圆", Font.LAYOUT_NO_LIMIT_CONTEXT, 76));
label.setForeground(Color.BLACK);
add(label);
setTitle("个性数字时钟");
setResizable(false);// 锁定窗口大小
setSize(320, 160);
setLocation(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
MyTime mt = new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread {
private MyTime mt;
public TimeThread(MyTime mt) {
this.mt = mt;
}
public void run() {
while (true) {
String fullTime = new Date().toString();
String time = fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener { // 内部类
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == mt.btn1)//btn1属于MyTime类
mt.label.setForeground(Color.RED);
else if ((JButton) e.getSource() == mt.btn2)
mt.label.setForeground(Color.GREEN);
else if ((JButton) e.getSource() == mt.btn3)
mt.label.setForeground(Color.BLUE);
}
}
}
//修改的地方已经注释
JAVA鼠标监听器问题!高手帮忙啊!
画图不能这样来画的,因为Graphics 不能自己来控制:
DrawLine DW=new DrawLine();
DW.g.setColor(Color.orange);
DW.g.drawLine(orgX, orgY, e.getX(), e.getY());
public void mouseRealeased(MouseEvent e)这个方法你拼错了,应该是:
public void mouseReleased(MouseEvent e)
帮你改了下,实现的功能:安下鼠标不放 -- 移动别处 -- 松开,画图
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;
public class DrawLine extends Frame {
static JPanel drawPanel;
static int orgX;
static int orgY;
static int eX;
static int eY;
public static void main(String[] args) {
// TODO 自动生成方法存根
drawPanel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawLine(orgX, orgY, eX, eY);
}
};
DrawLine mainFrame = new DrawLine();
mainFrame.setSize(400, 400);
mainFrame.setTitle("画线");
mainFrame.add(drawPanel);
mainFrame.setVisible(true);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
mainFrame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
eX = e.getX();
eY = e.getY();
drawPanel.repaint();
}
});
}
}
java 鼠标监听
//这是应用程序,跟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);
}
}
Java 鼠标监听事件 mouseMoved(MouseEvent)
public class BtnText1 extends JFrame implements MouseMotionListener
不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法
MouseAdapter类已经是实现了MouseMotionListener接口的。
改成
public class BtnText1 extends JFrame
可以运行成功
网站栏目:java监听鼠标代码 java监听鼠标位置
分享地址:http://azwzsj.com/article/hgscdh.html