我创建了一个 MouseMotionDetection 类,它的作用只是检测用户是否将鼠标移动到屏幕上的任何位置。
为此,我在我的类构造函数中创建了一个新的 JFrame,其屏幕尺寸是不可见的,所以基本上我在整个屏幕上观察鼠标运动。
但是,我有一个奇怪的错误:
在代码的当前形式中,一旦这个类被激活,我只检测到一个鼠标 Action ,没有别的,它在那之后立即停止工作。但是,如果我把将帧背景设置为 0f、0f、0f、0f(透明)的行放在评论中然后激活,整个屏幕就会变成灰色,我会按照我的需要继续跟踪所有鼠标 Action (我可以什么都看不到)。
我真的不明白为什么会这样,周围没有看到相关问题,也没有在这个相关javadoc, which discusses MouseMotion events.
这是代码:
public class MouseMotionDetection extends JPanel
implements MouseMotionListener{
public MouseMotionDetection(Region tableRegion, Observer observer){
addMouseMotionListener(this);
setBackground(new Color(0f,0f,0f,0f));
JFrame frame = new JFrame();
frame.setUndecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize);
frame.setBackground(new Color(0f,0f,0f,0f));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setAlwaysOnTop(true);
JComponent contentPane = this;
contentPane.setOpaque(true);
frame.getContentPane().add(contentPane, BorderLayout.CENTER);
frame.setVisible(true);
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent arg0) {
System.out.println("mouse movement detected");
}
最佳答案
完全透明的框架不接收鼠标事件。
这是使用 MouseInfo 的替代方法。这适用于应用程序的组件。不可见(透明)、未聚焦或最小化。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class MouseMoveOnScreen {
Robot robot;
JLabel label;
GeneralPath gp;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
MouseMoveOnScreen() throws AWTException {
robot = new Robot();
label = new JLabel();
gp = new GeneralPath();
Point p = MouseInfo.getPointerInfo().getLocation();
gp.moveTo(p.x, p.y);
drawLatestMouseMovement();
ActionListener al = new ActionListener() {
Point lastPoint;
@Override
public void actionPerformed(ActionEvent e) {
Point p = MouseInfo.getPointerInfo().getLocation();
if (!p.equals(lastPoint)) {
gp.lineTo(p.x, p.y);
drawLatestMouseMovement();
}
lastPoint = p;
}
};
Timer timer = new Timer(40, al);
timer.start();
}
public void drawLatestMouseMovement() {
BufferedImage biOrig = robot.createScreenCapture(
new Rectangle(0, 0, d.width, d.height));
BufferedImage small = new BufferedImage(
biOrig.getWidth() / 4,
biOrig.getHeight() / 4,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = small.createGraphics();
g.scale(.25, .25);
g.drawImage(biOrig, 0, 0, label);
g.setStroke(new BasicStroke(8));
g.setColor(Color.RED);
g.draw(gp);
g.dispose();
label.setIcon(new ImageIcon(small));
}
public JComponent getUI() {
return label;
}
public static void main(String[] args) throws Exception {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel ui = new JPanel(new BorderLayout(2, 2));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
try {
MouseMoveOnScreen mmos = new MouseMoveOnScreen();
ui.add(mmos.getUI());
} catch (AWTException ex) {
ex.printStackTrace();
}
JFrame f = new JFrame("Track Mouse On Screen");
// quick hack to end the frame and timer
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(ui);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
关于java - 检测屏幕上的鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25467866/
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht