急求大神帮忙!急!!!!!!!!

Qinvord 2013-09-02 08:07:53
MouseListener没有响应,不知道为什么,求大神帮帮忙,看哪不对啊
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

public class MouseTrack implements MouseListener{
private JFrame myFrame;
private JTextArea textArea;

public MouseTrack(){
myFrame=new JFrame();

JTextArea textArea=new JTextArea();
textArea.setEditable(false);
JScrollPane scroll=new JScrollPane(textArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

myFrame.addMouseListener(this);

myFrame.add(scroll,BorderLayout.CENTER);

myFrame.setSize(600,400);
myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}

public static void main(String args[]){
MouseTrack frame=new MouseTrack();
}

public void mouseClicked(MouseEvent e) {
System.out.println("***");
String button;
if(e.getButton()==e.BUTTON1){
button="左键";
}
else if(e.getButton()==e.BUTTON3){
button="右键";
}
else{
button="滚轮";
}
textArea.append("点击了"+button);
textArea.append("\n点击相对位置:x="+e.getX()+" y="+e.getY());
textArea.append("点击次数:"+e.getClickCount());
}

public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e) {};
}
...全文
106 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qinvord 2013-09-02
  • 打赏
  • 举报
回复
还有另外一个帖子,能帮我看看嘛?http://bbs.csdn.net/topics/390577793
Qinvord 2013-09-02
  • 打赏
  • 举报
回复
谢谢,监听对象弄错了
地下室森林 2013-09-02
  • 打赏
  • 举报
回复
package bbs0902;


import java.awt.BorderLayout;
import java.awt.event.*;

import javax.swing.*;

public class MouseTrack {
	private JFrame myFrame;
	private JTextArea textArea;

	public MouseTrack() {
		myFrame = new JFrame();

		final JTextArea textArea = new JTextArea();
		//textArea.setEditable(false);
		JScrollPane scroll = new JScrollPane(textArea);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		textArea.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.out.println("***");
				String button;
				if (e.getButton() == e.BUTTON1) {
					button = "左键";
				} else if (e.getButton() == e.BUTTON3) {
					button = "右键";
				} else {
					button = "滚轮";
				}
				textArea.append("点击了" + button);
				textArea.append("\n点击相对位置:x=" + e.getX() + " y=" + e.getY());
				textArea.append("点击次数:" + e.getClickCount() + "\n");
			}
		});

		myFrame.add(scroll, BorderLayout.CENTER);

		myFrame.setSize(600, 400);
		//myFrame.setResizable(false);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myFrame.setVisible(true);
	}

	public static void main(String args[]) {
		MouseTrack frame = new MouseTrack();
	}
}
MiceRice 2013-09-02
  • 打赏
  • 举报
回复
加这句再试: textArea.addMouseListener(this);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧