Java 程序实现鼠标点击 键盘等事件
这个用MouseListener里的mouseEntered(MouseEvent
e)方法。
鼠标进入区域就产生事件反应。
如果之前申明了点击事件可以在entered时间里呼叫clicked事件。
关于Java中鼠标事件处理的问题,我的代码
你这样不行啊,一闪一闪的根本看不到显示效果或者更本没有显示,绘图的方法要写在paintComponent(Graphics g)里面才能长久保存。
我知道你想在mouseClicked里面做什么,但是在这里进行绘图,repaint没有立即被执行,所以有可能没有任何显示;如果repaint执行了,也很有可能立刻被后面的绘图所覆盖。最好的办法是在这里出发一个标志,然后调用repaint函数,在paintComponent函数中对这个标志进行绘制,决定到底该怎么画。
手写了一段代码,没有编译,你可以参考一下:
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class JXFrame extends JFrame
{
boolean mouseCliked = false;
int x,y;
public void paintComponent(Graphics g)
{
if(mouseCliked )
{
g.setColor(255,255,0);//必须有这一步,否则画笔颜色为默认背景色,是根本看不出任何线条效果的。
g.drawLine(0,0,x,y);
}
}
public void setPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
package colin;
import java.awt.*;
import javax.swing.*;
class frame{
frame(){
JXFrame f = new JXFrame();
f.addMouseListener(new Mouseevent(f));
f.setSize(500,600);
f.setVisible(true);
}
public static void main(String args[]){
new frame();
}
}
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Mouseevent implements MouseListener{
JXFrame parent;
public Mouseevnt(JXFrame parent){
this.parent = parent;
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseEntered(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseExited(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseClicked(MouseEvent e){
this.parent.setPoint(e.getX(),e.getY());
this.parent.mouseCliked = true;
this.parent.repaint();
}
}
java鼠标点击事件
给你一个例子,太难讲了
我自己写的
package guidemo;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* pTitle: 图形用户界面/p
*
* pDescription: 简单的图形界面编程/p
*
* pCopyright: Copyright (c) 2006/p
*
* pCompany: /p
*
* @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写一个单击鼠标事件
使用组件的paint函数用于绘图, 使用MouseListener来响应鼠标的点击
效果图
代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class DemoWin extends JFrame {
public DemoWin() {
MyPanel mp = new MyPanel();
mp.addMouseListener(mp);
add(mp);
// 窗口属性设置
setTitle("Demo");// 标题
setSize(300, 280);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口点击关闭时,退出程序
}
public static void main(String[] args) {
DemoWin win = new DemoWin(); // 创建窗口
win.setVisible(true);// 显示窗口
}
class MyPanel extends JPanel implements MouseListener {
int times;// 记录点击的次数
int x;// 记录鼠标X轴的位置
int y;// 记录鼠标Y轴的位置
@Override
public void paint(Graphics g) {
super.paint(g);
if (times == 0) {
g.setColor(Color.BLUE);// 颜色
g.fillOval(150, 150, 50, 50);// 150,150代表位置 50,50代表宽高
} else if (times == 1) {
g.setColor(Color.RED);
g.fillRect(150, 150, 50, 50);
} else {
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
repaint();
}
public void mouseClicked(MouseEvent e) {
// if (e.getButton()==MouseEvent.BUTTON1) {//单击左键时有效..
// times++;// 记录点击的次数
// x = e.getX();
// y = e.getY();
// }
}
public void mousePressed(MouseEvent e) {// 鼠标按下就有效
times++;// 记录点击的次数
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {// 鼠标释放
}
public void mouseEntered(MouseEvent e) {// 鼠标移入
}
public void mouseExited(MouseEvent e) {// 鼠标移出
}
}
}
Java 点击鼠标拖动某一个点代码
this.addMouseListener(new
MouseAdapter()
{
@Override
public
void
mousePressed(MouseEvent
e)
{
x
=
e.getX();
y
=
e.getY();
System.out.println("Xbefore:"+e.getX());
}
});
this.addMouseMotionListener(new
MouseMotionListener()
{
public
void
mouseDragged(MouseEvent
e)
{
JButton
btn
=
(JButton)
e.getSource();
setLocation(btn.getX()
+
e.getX()
-
x,
btn.getY()
+
e.getY()
-
y);
}
public
void
mouseMoved(MouseEvent
e)
{
}
});
上面的代码是给JButton添加的鼠标事件,移动的是按钮。。。
java中MouseEvent事件例子
public class MouseClickEvent extends JFrame{
public MouseClickEvent() {
Container container = getContentPane();
container.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("释放的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("释放的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("释放的是鼠标右键!\n");
break;
}
}
public void mousePressed(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("按下的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("按下的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("按下的是鼠标右键!\n");
break;
}
}
public void mouseExited(MouseEvent arg0) {
System.out.println("光标移出应用程序窗口了!\n");
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("光标进入应用程序窗口了!\n");
}
public void mouseClicked(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("点击的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("点击的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("点击的是鼠标右键!\n");
break;
}
int clickCount = arg0.getClickCount();
System.out.println("点击次数为"+clickCount+"\n");
}
});
}
public static void main(String[] args) {
MouseClickEvent testFrame = new MouseClickEvent();
testFrame.setTitle("鼠标点击事件测试程序!");
testFrame.setVisible(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setBounds(0, 0, 500, 400);
}
}