当前位置:首页 » 编程语言 » java监听鼠标

java监听鼠标

发布时间: 2022-06-17 09:41:38

java 鼠标监听器

JMenuItem againItem = startFile.add("再玩一次");//建立子菜单
JMenuItem heroItem = startFile.add("英雄榜");
JMenuItem howItem = helpFile.add("如何游戏");
JMenuItem writerItem = aboutFile.add("制作组成员");
JMenuItem callItem = aboutFile.add("联系方式");

againItem.addActionListener(new ActionHandler());//建立鼠标监听器

}
}

class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"Hello world!","Display",JOptionPane.INFORMATION_MESSAGE);
}
}

或者:

JMenuItem againItem = startFile.add("再玩一次");//建立子菜单
JMenuItem heroItem = startFile.add("英雄榜");
JMenuItem howItem = helpFile.add("如何游戏");
JMenuItem writerItem = aboutFile.add("制作组成员");
JMenuItem callItem = aboutFile.add("联系方式");

againItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"Hello world!","Display",JOptionPane.INFORMATION_MESSAGE);
}
}
);//建立鼠标监听器

}
}

Ⅱ 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监听鼠标事件的问题

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame implements Runnable{
public static int x,y,direction=0;
public test(){
this.setSize(600,400);
this.setVisible(true);
x=this.getContentPane().getWidth()/2;
y=this.getContentPane().getHeight()/2;
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:
direction=1;break;
case KeyEvent.VK_LEFT:
direction=2;break;
case KeyEvent.VK_RIGHT:
direction=3;break;
case KeyEvent.VK_UP:
direction=4;break;
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
test t=new test();
new Thread(t).start();
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.drawRect(x-60,y-40,120,80);
}
public void run(){
while(true){
try{
Thread.sleep(100);
}catch(Exception e){}
switch(direction){
case 1: y =4;break;
case 2: x-=4;break;
case 3: x =4;break;
case 4: y-=4;break;
}
this.repaint();
}
}
}

Ⅳ Java中关于鼠标监听器的一点问题

1、我理解不存在拖动这一说,不管是点击还是使用键盘,都是图片坐标的问题。

2、宫格数是二维数组,我们初始化记录空白块的坐标,此时监听鼠标的活动事件,判断目标位置坐标与空白块坐标有相邻一组的重合即运动块的位置是合法的,然后我们交换空白块与运动块的坐标就可以实现所谓的“拖动”。

3、代码

参考

Ⅳ Java中要监听鼠标事件,则实现监听器类可以是使用的方式有哪几种

,MouseWheelListener,MouseMotionListener

如上所示,监听鼠标事件只要使用MouseAdapter类就行了

Ⅵ java鼠标监听器问题

实现鼠标监听借口,并给相应的控件进行监听,然后鼠标keyUp方法里进行f.setVisible(false);

Ⅶ Java 鼠标监听事件 mouseMoved(MouseEvent)

不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法

MouseAdapter类已经是实现了MouseMotionListener接口的。

改成

可以运行成功

Ⅷ 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中的鼠标监听

this.addMouseListener(this);意味着本身就是一个MouseListener的子类,如果把this换成其他MouseListener的实现就不需要继承了。

Ⅹ JAVA鼠标监听

楼主,你没有将number定义成静态成员吧。没有将它定义为静态成员,结果当然是零。

热点内容
津贴脚本 发布:2025-02-06 19:44:10 浏览:739
好分数里如何修改密码 发布:2025-02-06 19:42:30 浏览:155
mysql存储过程判断 发布:2025-02-06 19:40:15 浏览:852
bat编译器的作用 发布:2025-02-06 19:26:54 浏览:343
phpajaxsession 发布:2025-02-06 19:20:56 浏览:623
西安java学习 发布:2025-02-06 19:15:44 浏览:622
微信电影源码网站 发布:2025-02-06 18:55:21 浏览:934
本地建mysql数据库 发布:2025-02-06 18:54:23 浏览:762
屏幕看不清了如何输密码 发布:2025-02-06 18:51:14 浏览:333
手机开脚本买个什么配置的 发布:2025-02-06 18:45:59 浏览:113