我正在尝试添加一些代码,当鼠标移动一定次数时启动计时器,下面的代码用于鼠标移动。我希望计时器持续 10 秒并提醒用户计时器已启动和结束。
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);
}
}
最佳答案
也许我在这里遗漏了一些东西,但你要求的似乎是一个相当简单的任务。我猜你的问题是设置“计时器”本身? java.util.Timer 类可以用于此目的。
因此,对于您的情况,类似的函数
private void startTimer()
{
isTimerRunning = true;
new java.util.Timer().schedule(new java.util.TimerTask()
{
@Override
public void run()
{
isTimerRunning = false;
}
}, 10000);
}
您必须像这样从 mouseMoved 函数中调用此函数,
public void mouseMoved(MouseEvent e)
{
eventOutput("Mouse moved", e);
if (!isTimerRunning)
{
startTimer();
}
}
您可以将警报代码与设置和重置 isTimerRunning 的代码一起放置。
编辑: 正如 VGR 所提到的,javax.swing.Timer 更适合与其他 swing 组件一起使用,特别是在执行与 GUI 相关的操作时。 从文档中,
In general, we recommend using Swing timers rather than general-purpose timers for GUI-related tasks because Swing timers all share the same, pre-existing timer thread and the GUI-related task automatically executes on the event-dispatch thread. However, you might use a general-purpose timer if you don't plan on touching the GUI from the timer, or need to perform lengthy processing.
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);
}
}
关于java - 尝试在检测到鼠标移动时添加计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614766/