2017-01-12 6 views
0

マウスを一定回数動かしたときにタイマーを開始するコードを追加しようとしています。以下のコードはマウスの動きです。タイマーを10秒間持続させ、タイマーが開始して終了したことをユーザーに警告します。マウスの動きが検出されたときにタイマーを追加しようとしました

ここで何かが欠けてI'amたぶん
public class MouseMotionEvent extends JPanel 
    implements MouseMotionListener { 
BlankArea blankArea; 
JTextArea textArea; 
static final String NEWLINE = System.getProperty("line.separator"); 

public static void main(String[] args) { 

    try { 

     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } catch (UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } catch (IllegalAccessException ex) { 
     ex.printStackTrace(); 
    } catch (InstantiationException ex) { 

     ex.printStackTrace(); 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } 

    UIManager.put("swing.boldMetal", Boolean.FALSE); 


    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 


private static void createAndShowGUI() { 

    JFrame frame = new JFrame("MouseMotionEventDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    JComponent newContentPane = new MouseMotionEvent(); 
    newContentPane.setOpaque(true); 
    frame.setContentPane(newContentPane); 


    frame.pack(); 
    frame.setVisible(true); 
} 

public MouseMotionEvent() { 
    super(new GridLayout(0,1)); 
    blankArea = new BlankArea(Color.YELLOW); 
    add(blankArea); 

    textArea = new JTextArea(); 
    textArea.setEditable(false); 
    JScrollPane scrollPane = new JScrollPane(textArea, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    scrollPane.setPreferredSize(new Dimension(200, 75)); 

    add(scrollPane); 

    blankArea.addMouseMotionListener(this); 
    addMouseMotionListener(this); 

    setPreferredSize(new Dimension(450, 450)); 
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); 
} 

void eventOutput(String eventDescription, MouseEvent e) { 
    textArea.append(eventDescription 
      + " (" + e.getX() + "," + e.getY() + ")" 
      + " detected on " 
      + e.getComponent().getClass().getName() 
      + NEWLINE); 
    textArea.setCaretPosition(textArea.getDocument().getLength()); 
} 

public void mouseMoved(MouseEvent e) { 
    eventOutput("Mouse moved", e); 
} 

public void mouseDragged(MouseEvent e) { 
    eventOutput("Mouse dragged", e); 
} 

}

+0

小さなサイドノート:JTextAreasは常に行の区切りではなく、システムの電源ラインとして '\のN 'を使用しますセパレータ。 – VGR

答えて

1

、しかし、あなたが求めることはかなりまっすぐな作業のように思えます。私はあなたの問題を推測することは "タイマー"自体を設定することです? この目的でjava.util.Timerクラスを使用できます。あなたが一緒にあなたのアラートコードを置くことができ

public void mouseMoved(MouseEvent e) 
{ 
    eventOutput("Mouse moved", e); 
    if (!isTimerRunning) 
    { 
     startTimer(); 
    } 
} 

、あなたがこのようなのmouseMoved関数内からこの関数を呼び出す必要があります

private void startTimer() 
{ 
    isTimerRunning = true; 
    new java.util.Timer().schedule(new java.util.TimerTask() 
    { 
     @Override 
     public void run() 
     { 
      isTimerRunning = false; 
     } 
    }, 10000); 

} 

よう

だから、あなたのケースのために、機能、設定してリセットするコードはisTimerRunningです。

EDIT: VGRで述べたように、のjavax.swing.Timerは特に関連の何かのGUIをやったときに、他のSwingコンポーネントと一緒に使用するためのより良いです。スイングタイマーはすべて自動的に同じ、既存のタイマースレッドとGUI関連のタスクを共有するためのドキュメントから 、一般的には

は、我々は、GUI関連のタスクのためのスイングタイマーではなく、汎用タイマを使用することをお勧めしますイベントディスパッチスレッド上で実行されます。ただし、タイマーからGUIに触れることを計画していない場合、または時間のかかる処理を実行する必要がある場合は、汎用タイマを使用することができます。

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

使用するように変更あなたのコード、のjavax.swing.Timer、

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class MouseMotionEvent extends JPanel implements MouseMotionListener 
{ 
BlankArea blankArea; 
JTextArea textArea; 
private Timer timer; 
boolean isTimerRunning = false; 
static final String NEWLINE = System.getProperty("line.separator"); 

public static void main(String[] args) 
{ 
    try 
    { 

     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } 
    catch (UnsupportedLookAndFeelException ex) 
    { 
     ex.printStackTrace(); 
    } 
    catch (IllegalAccessException ex) 
    { 
     ex.printStackTrace(); 
    } 
    catch (InstantiationException ex) 
    { 

     ex.printStackTrace(); 
    } 
    catch (ClassNotFoundException ex) 
    { 
     ex.printStackTrace(); 
    } 

    UIManager.put("swing.boldMetal", Boolean.FALSE); 

    javax.swing.SwingUtilities.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      createAndShowGUI(); 
     } 
    }); 
} 

private static void createAndShowGUI() 
{ 

    JFrame frame = new JFrame("MouseMotionEventDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JComponent newContentPane = new MouseMotionEvent(); 
    newContentPane.setOpaque(true); 
    frame.setContentPane(newContentPane); 

    frame.pack(); 
    frame.setVisible(true); 
} 

public MouseMotionEvent() 
{ 
    super(new GridLayout(0, 1)); 
    blankArea = new BlankArea(Color.YELLOW); 
    add(blankArea); 

    textArea = new JTextArea(); 
    textArea.setEditable(false); 
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    scrollPane.setPreferredSize(new Dimension(200, 75)); 

    add(scrollPane); 

    blankArea.addMouseMotionListener(this); 
    addMouseMotionListener(this); 

    setPreferredSize(new Dimension(450, 450)); 
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 

    ActionListener action = new ActionListener() 
    { 
     @Override 
     public void actionPerformed(ActionEvent event) 
     { 
      timer.stop(); 
     } 
    }; 

    timer = new Timer(0, action); 
    timer.setInitialDelay(10000); 
} 

void eventOutput(String eventDescription, MouseEvent e) 
{ 
    textArea.append(eventDescription + " (" + e.getX() + "," + e.getY() + ")" + " detected on " + e.getComponent().getClass().getName() + NEWLINE); 
    textArea.setCaretPosition(textArea.getDocument().getLength()); 
} 

public void mouseMoved(MouseEvent e) 
{ 
    eventOutput("Mouse moved", e); 
    if (!timer.isRunning()) 
    { 
     timer.start(); 
    } 
} 

public void mouseDragged(MouseEvent e) 
{ 
    eventOutput("Mouse dragged", e); 
} 
} 
+0

Swingアプリケーションでは、javax.swing.Timerはほとんどの場合、java.util.Timerよりも優れた選択肢です。 – VGR

+0

@VGRは実際にユースケースに依存します...タイマーがGUI関連のものを実行する場合、はい、javax.swing.Timerはより良いでしょうが、バックグラウンドタスクの場合、実際には関係ありません...私は更新します情報を含める答えは...ありがとう! –

関連する問題