java鼠标点击事件怎么做?

java鼠标点击事件~

给你一个例子,太难讲了
我自己写的
package guidemo;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
* Title: 图形用户界面

*
*
Description: 简单的图形界面编程

*
*
Copyright: Copyright (c) 2006

*
*
Company:
*
* @author vic
* @version 1.0
*/
public class ColorFrame extends Frame implements MouseListener {
Label L; //标签
TextField T; //文本域
Button B1, B2; //按钮
public ColorFrame() {
this.setLayout(null); //想要手动指定各组件的的位置
L = new Label("输入学号:"); //设定标签L内容
L.setBounds(60, 50, 50, 25); //设定标签L外观
this.add(L); //将标签L添加到窗口中
T = new TextField("请在这里输入"); //设定文本域T的内容
T.setBounds(125, 50, 90, 25); //设定文本域T的外观
this.add(T); //将文本域T添加到窗口中
B1 = new Button("变红!"); //设定按钮B1的内容
B1.setBounds(25, 90, 90, 25); //设定按钮B1的外观
B1.addMouseListener(this);//在B1上注册鼠标监听器
this.add(B1); //将按钮B1添加到窗口中
B2 = new Button("变绿!");
B2.setBounds(125, 90, 90, 25);
B2.addMouseListener(this);
this.add(B2);
WindowDestroyer Listener = new WindowDestroyer(); //创建关闭窗口监听器
this.addWindowListener(Listener); //将监听器添加到窗口中
this.setBackground(Color.yellow); //设定窗口背景颜色
this.setTitle("This is Frame!"); //设定窗口标题文字
this.setBounds(0, 0, 250, 220); //设定窗口位置和大小
this.setVisible(true); //显示窗口
}

public void mouseClicked(MouseEvent e) {
if (e.getComponent() == B1) {//getComponent返回按钮上面的字符串
this.setBackground(Color.red);
}
if (e.getComponent() == B2) {
this.setBackground(Color.green);
}
}

public void mouseExited(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public static void main(String[] args) {
new ColorFrame();
}
}
希望能解决您的问题。

在面板外面区域加一个获得焦点事件,当你鼠标点外面的时候,会触发这个事件。事件里面就写隐藏这个面板的代码。或者给这个面板加一个失去焦点的事件。

java鼠标点击事件的方法如下:

<span style="font-family:Verdana;">事件源</span>.addMouseListener(new MouseAdapter() {//建立事件处理机制  
   @Override  
   public void mouseClicked(MouseEvent e) {  
       if(e.getButton()==e.BUTTON1){//点击鼠标左键  
           int x=e.getX();  
           int y=e.getY();  
           String str="您点击的是左键,鼠标当前点击位置的坐标是(" + x + "," + y+")";  
           label.setText(str);  
       }else if(e.getButton()==e.BUTTON2){//点击鼠标滑轮  
           int x=e.getX();  
           int y=e.getY();  
           String str="您点击的是滑轮,鼠标当前点击位置的坐标是(" + x + "," + y+")";  
           label.setText(str);   
       }  
       else if(e.getButton()==e.BUTTON3){//点击鼠标右键  
           int x=e.getX();  
           int y=e.getY();  
           String str="您点击的是右键,鼠标当前点击位置的坐标是(" + x + "," + y+")";  
           label.setText(str);       
       }  
   }  
});

e.getButton()返回值分别为NOBUTTON、BUTTON1、BUTTON2、BUTTON3,分别代表着无点击、左击、中间键、右击三种情况。



Java,鼠标单击jtable某行,鼠标单击事件
答:最主要的事件处理给你:jTable.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent e) { if(jTable.getValueAt(jTable.getSelectedRow(),0)!=null){ String s = (String) jTable.getValueAtjTable.getSelectedRow(),0); //获取所...

请问java中怎么实现鼠标经过事件?
答:在每个格点上加透明组件,组件的大小就是你想圈出一个区域的大小,并给组件加上鼠标经过事件,这样就简单多了。如果不想在格点上做组件,那就计算出所有格点的圈出区域的坐标,假设每个区域都是正方形的(非圆形),那么每个格点就是有4个坐标集合(x,x+长,y,y+宽)。取窗体鼠标移动事件,获取...

如何用JAVA编一个 鼠标在在屏幕上移动的 并点击的 程序?
答:楼主提问的水平很高啊,题目的要求都没有,如何回答。。。相关类都在 import java.awt.*;import javax.swing.* ;动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了 给一个相关的代码吧,包括所有的鼠标事件。下面代码包括 按下,释放,进入区域,离开区域,点击 这5个事件 ...

JAVA中设置鼠标点击事件怎么设置啊?为什么没反应?跪谢
答:代码是图片逐句逐句看太麻烦了~ ~,根据你的意思直接做了个差不多的,代码如下 import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.* ;public class Student { public static void main(String args[]){ JFrame jf = new JFrame() ;jf.setVisible(true) ;...

Java获取窗口鼠标坐标以及键盘按键
答:Java中的鼠标和键盘事件 使用MouseListener借口处理鼠标事件 鼠标事件有 种 按下鼠标键 释放鼠标键 点击鼠标键 鼠标进入和鼠标退出 鼠标事件类型是MouseEvent 主要方法有 getX() getY() 获取鼠标位置 getModifiers() 获取鼠标左键或者右键 getClickCount() 获取鼠标被点击的次数 getSource() 获取鼠标发生的...

java中如何获取网页中鼠标点击过的事件
答:先获取网页中的元素,在处理鼠标的事件 mousedown:鼠标按钮被按下(左键或者右键)时触发。不能通过键盘触发。mouseup:鼠标按钮被释放弹起时触发。不能通过键盘触发。click:单击鼠标左键或者按下回车键时触发。这点对确保易访问性很重要,意味着onclick事件处理程序既可以通过键盘也可以通过鼠标执行。dblclick...

java中控制鼠标点击的方法
答://程序流程:模拟鼠标左键点击 开始-->运行-->CMD-->DIR-->CLS-->EXIT //模拟右键点击 移动到右下角-->右键点击-->调整日期和时间-->退出 import java.awt.*;import java.awt.event.*;import javax.swing.JOptionPane;//Test.java public class Test { public static void main(String[] ...

Java 怎样用mouseMoved()实现鼠标移动触发事件
答:import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import javax.swing.JButton;import javax.swing.JFrame;public class BuTest implements MouseMotionListener{ JFrame jf=new JFrame("在按钮上移动的文字");JButton[] button=new JButton[9];int mount=0;public void ...

关于JAVA中鼠标单击按钮事件的问题
答:B1.addMouseListener(this);//在B1上注册鼠标监听器 this.add(B1); //将按钮B1添加到窗口中 B2 = new Button("变绿!");B2.setBounds(125, 90, 90, 25);B2.addMouseListener(this);this.add(B2);WindowDestroyer Listener = new WindowDestroyer(); //创建关闭窗口监听器 this.addWindow...

这鼠标事件用java怎么写,望大神帮忙
答:serialVersionUID=2918L;private JLabel n;MouseDemo(){this.setTitle("鼠标侦测");this.setResizable(false);this.setBounds(400,500,500,200);this.setDefaultCloseOperation(3);this.setLayout(new FlowLayout());init();this.setVisible(true);}private void init() {this.add(new JLabel(...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网