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();
}
}
希望能解决您的问题。

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

//我有个差不多的例子..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LabelTest
{

JFrame frame = new JFrame("JButtonTest");
int len = 4;
JButton[] lbs = new JButton[len];
GridLayout glayout = new GridLayout(len,1);
boolean hc = true;

public void lunachFrame(){
frame.setLayout(glayout);
frame.setLocation(200, 200);
for(int i = 0; i < len ; i ++){
lbs[i] = new JButton("我是Button" + (i + 1));
final int n = i;
lbs[i].addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){
if(hc){
lbs[n].setBackground(Color.RED);
hc = !hc;
}
else{
lbs[n].setBackground(Color.BLUE);
hc = !hc;
}
}
});
frame.add(lbs[i]);
}
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args)
{
new LabelTest().lunachFrame();
}
}

你看看JLabel能不能加一个MouseListener事件!
然后写一下这个接口的相应的事件处理方法!

上面所言即是。

JLabel的对象.addMouseListener(实现MouseListener类对象);
如果你已经做了上面这步的话。。。发代码上来看看吧

java鼠标点击事件怎么做?
答:java鼠标点击事件的方法如下:事件源.addMouseListener(new MouseAdapter() {//建立事件处理机制 @Override public void mouseClicked(MouseEvent e) { if(e.getButton()==e.BUTTON1){//点击鼠标左键 int x=e.getX(); int y=e.getY(); String str="您点击的是左键,鼠标当...

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

java JFrame中MouseClicked有时无响应
答:1、你的事件没有触发,也就是说,点击的地方不对,这个估计你自己能解决 2、程序某段长代码正在执行,导致没响应,解决办法,使用线程。发生点击事件的时候,就建立一个线程并且start()界面中图像的变换,同样要使用线程,否则给人的感觉就是 “卡”,长时间没响应 ...

用java在文本框实现鼠标点击事件,一点文本框直接跳出新对话框
答:import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFrame;import javax.swing.JTextField;public class Exec1 extends JFrame { public Exec1() { JTextField txt = new JTextField();txt.addMouseListener(new MouseListener() { public void mouseClicked(MouseE...

如何区分java中单击的是左击还是右击,还有一个问题是能否取消鼠标的监听...
答:鼠标事件要添加MouseListener,捕获MouseEvent。MouseListener里的几个事件的参数都是MouseEvent,MouseEvent提供了获得点击了哪个键的方法getButton。比如下面这个点击事件处理。 public void mouseClicked(MouseEvent arg0) { if(arg0.getButton() == MouseEvent.BUTTON1) { // 左键点击 } else if(arg...

Java JTable 添加了一个键盘事件,和鼠标点击事件的监听,如何在我键盘...
答:1.鼠标右键点击的事件前加个if判断:if(canClick){ 鼠标点击事件事件 } 2.然后添加键盘事件implements KeyListener 在重写的方法的keyPressed中加入:if(e.getKeyCode()==KeyEvent.VK_CONTROL){ canClick=false;} 在重新的方法的keyReleased中加入:if(e.getKeyCode()==KeyEvent.VK_CONTROL){ can...

java 我在按钮那里设了一个鼠标事件,如何实现点击一下该按钮后取消它...
答:可以用removeActionListener方法来取消,但这可能会有后遗症,以后若在需要此事件时还要再addActionListener方法,比较理想的方案是定义个boolean变量,根据true或false来决定是否运行事件中的代码而不要取消掉事件。

java用鼠标点击Button产生的事件是什么?
答:你说的是鼠标么??点击的时候就有三种事件:void mouseClicked(MouseEvent e)鼠标按键在组件上单击(按下并释放)时调用。void mousePressed(MouseEvent e)鼠标按键在组件上按下时调用。mouseReleasedvoid mouseReleased(MouseEvent e)鼠标按钮在组件上释放时调用。你问的是哪一件??这个其实可以查java的...

Java AWT 中Button 鼠标点击事件是那个?
答:ActionListener)对象,ActionListener是一个接口,你重写ActionPerformed方法,这个接口就这一个方法,这个方法的参数是一个ActionEvent对象。调用Button对象的addActionListener方法添加监听器,当发生鼠标点击事件的时候,awt会把事件封装成一个对象传给监听器,在监听器中的ActionPerformed中处理事件。

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) ;jf.setSize(500,300) ;JPanel jp = new JPanel() ;jf.setContentPane(jp) ...

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

联系反馈
Copyright© IT评价网