Java:如何保存MouseButton状态?

时间:2014-04-21 16:06:37

标签: java logging mouse

所以我正在编写一个程序来保存鼠标按钮(当前只是mousePressed,但会将mouseReleased一旦生效)将状态保存到日志文件中。我怎么做? 目前的代码:

import static com.sun.java.accessibility.util.AWTEventMonitor.addMouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class mouse {
public static void main(String[] args) throws InterruptedException{
mouse();

} 
public static void mouse() throws InterruptedException{
for(int i=0;i<10000;i++){
    addMouseListener(new MouseAdapter() { 
        public void mousePressed(MouseEvent me) { 
        writeToFile(me);    //writeToFile saves the me value to txt
        } 
       });
    TimeUnit.MILLISECONDS.sleep(1); // time delay, so it saves every 1ms
}
}


public static void writeToFile(MouseEvent me){

try{
    String data =" "+me+" ";

    File file =new File("log.txt");

    //if log.txt doesn't exist, it makes it
    if(!file.exists()){
        file.createNewFile();
    }

    //true = write
        FileWriter fileWritter = new FileWriter(file.getName(),true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write(data);
        bufferWritter.close();


    }catch(IOException e){
    }
}

}

程序编译并运行,但它没有做任何事情 我提前感谢你们。

1 个答案:

答案 0 :(得分:1)

如果您没有尝试创建GUI而是想要记录鼠标的一般位置,那么在您的课程中添加MouseListener将无济于事。相反,要了解Java并未针对此类程序进行优化,您可能必须使用JNA或JNI来捕获操作系统的鼠标处理代码。另一方面,如果您希望捕获鼠标使用Swing GUI,那么首先需要创建一个Swing GUI,然后将MouseListener添加到它。我建议您阅读MouseListener教程以获取更多相关信息。


修改
你说:

  

最终,它将成为GUI应用程序的一部分,我只想尝试制作它的概念。

请理解,如果没有将MouseListener添加到接受它的Component,它将无法正常工作,最终会从GUI中显示的Component类型扩展。因此,为了测试您的概念,您将再次需要创建一个Swing GUI并将MouseListener添加到GUI的一个可视化组件中。请再次阅读MouseListener tutorials


  

关于覆盖文件,它没有,我从我以前的程序中取出它,它运行正常。

抱歉,我的错 - 我误读了你的代码。


编辑2
并且如评论中所述,听众不应该有循环,而是应该响应鼠标 EVENTS 。例如,以下代码侦听鼠标按下,释放和拖动,并在JTextArea中显示信息。将此更改为写入文件将是微不足道的:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class SimpleMouseListenerEg extends JPanel {
   private static final int PREF_W = 600;
   private static final int PREF_H = 400;
   private JPanel listenedToPanel = new JPanel();
   private JTextArea textArea = new JTextArea(10, 25);

   public SimpleMouseListenerEg() {
      listenedToPanel.setBorder(BorderFactory.createTitledBorder("Listened To Panel"));
      MouseAdapter myMouseAdapter = new MyMouseAdapter();
      listenedToPanel.addMouseListener(myMouseAdapter);
      listenedToPanel.addMouseMotionListener(myMouseAdapter);

      textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
      JScrollPane scrollPane = new JScrollPane(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      setLayout(new BorderLayout());
      add(listenedToPanel, BorderLayout.CENTER);
      add(scrollPane, BorderLayout.EAST);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   // a combination MouseListener and MouseMotionListener
   private class MyMouseAdapter extends MouseAdapter {
      private String template = "%-10s [%03d, %03d]%n";

      @Override
      public void mousePressed(MouseEvent mEvt) {
         String text = String.format(template, "Pressed", mEvt.getX(), mEvt.getY());
         textArea.append(text);
      }

      @Override
      public void mouseReleased(MouseEvent mEvt) {
         String text = String.format(template, "Released", mEvt.getX(), mEvt.getY());
         textArea.append(text);
      }

      @Override
      public void mouseDragged(MouseEvent mEvt) {
         String text = String.format(template, "Dragged", mEvt.getX(), mEvt.getY());
         textArea.append(text);
      }
   }

   private static void createAndShowGui() {
      SimpleMouseListenerEg mainPanel = new SimpleMouseListenerEg();

      JFrame frame = new JFrame("SimpleMouseListenerEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}