Czas naciśniętego przycisku

0

Witam, mam pytanie. Czy można zrobić coś takiego że użytkownik musi przytrzymać przycisk przynajmniej 5 sekund żeby zadziałał?

0

nie jestem do końca pewien ale można to chyba zrobić timerem w obsłudze klawisza...

0

Nie było to takie zajmujące jak się wydawało.

package Olamagato.Swing;

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import javax.swing.DefaultButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.Action;
import javax.swing.Timer;

/**
 * Button that require press and hold 5 second (default) for launch its
 * action. Hold time can be get by getHoldDelay and change by setHoldDelay.
 * @author Olamagato
 * @version 1.01
 */
public class JHoldButton extends JButton
{
	/** */
	public final int DEFAULT_HOLD_DELAY = 5000; //ms

	/**
	 * Creates a button with no set text or icon.
	 */
	public JHoldButton()
	{
		this(null, null);
	}

	/**
	 * Creates a button with an icon.
	 *
	 * @param icon  the Icon image to display on the button
	 */
	public JHoldButton(Icon icon)
	{
		this(null, icon);
	}

	/**
	 * Creates a button with text.
	 *
	 * @param text  the text of the button
	 */
	public JHoldButton(String text)
	{
		this(text, null);
	}

	/**
	 * Creates a button where properties are taken from the
	 * Action supplied.
	 *
	 * @param a the Action used to specify the new button
	 *
	 * @since 1.3
	 */
	public JHoldButton(Action a)
	{
		this();
		setAction(a);
	}

	/**
	 * Creates a button with initial text and an icon.
	 *
	 * @param text  the text of the button
	 * @param icon  the Icon image to display on the button
	 */
	public JHoldButton(String text, Icon icon)
	{
		// Create the model
		setModel(new HoldButtonModel());
		// initialize
		init(text, icon);
	}

	/**
	 * Set new required hold time
	 * @param delay time period in ms
	 */
	public void setHoldDelay(int delay)
	{
		holdDelay = delay;
	}

	/**
	 * Get required hold time
	 * @return time period in ms
	 */
	public int getHoldDelay() { return holdDelay; }

	/** Changed button behavior */
	private class HoldButtonModel extends DefaultButtonModel
	{
		@Override public void setPressed(boolean b)
		{
			if((isPressed() == b) || !isEnabled())
				return;
			if(b)
			{
				stateMask |= PRESSED;
				JHoldButton.this.enableCountdownState();
			}
			else
			{
				stateMask &= ~PRESSED;
				JHoldButton.this.disableCountDownState();
			}
			if(!isPressed() && isArmed() && JHoldButton.this.isOnTime())
			{
				int modifiers = 0;
				AWTEvent currentEvent = EventQueue.getCurrentEvent();
				if(currentEvent instanceof InputEvent)
					modifiers = ((InputEvent)currentEvent).getModifiers();
				else if(currentEvent instanceof ActionEvent)
					modifiers = ((ActionEvent)currentEvent).getModifiers();
				fireActionPerformed(new ActionEvent(this,
					ActionEvent.ACTION_PERFORMED, getActionCommand(),
					EventQueue.getMostRecentEventTime(), modifiers));
			}
			fireStateChanged();
		}
		private static final long serialVersionUID = 7103271105110148L;
	}

	/** */
	private void enableCountdownState()
	{
		originalText = JHoldButton.this.getText();
		int secondRest = holdDelay % SEC;
		updateCounter(holdDelay + (secondRest > 0 ? SEC : 0));
		stopper.setInitialDelay(secondRest);
		pressed = holdDelay - secondRest;
		stopper.start();
	}

	/** */
	private void disableCountDownState()
	{
		stopper.stop();
		JHoldButton.this.setText(originalText);
	}

	/** */
	private ActionListener countDown = new ActionListener()
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			updateCounter(pressed);
			if(isOnTime())
			{
				stopper.stop();
				pressed = -1;
			}
			else
				pressed -= SEC;
		}
	};

	/** Show time */
	private void updateCounter(int ms)
	{
		if(ms < 0) ms = 0;
		JHoldButton.this.setText(originalText + " (" + (ms / SEC) + ")");
	}

	/** Test for fire action and stop timer */
	private boolean isOnTime()
	{
		return pressed < 0;
	}

	private static final int SEC = 1000;
	private Timer stopper = new Timer(SEC, countDown);
	private String originalText;
	private int pressed = 0;
	private int holdDelay = DEFAULT_HOLD_DELAY;
	private static final long serialVersionUID = 7103271105110140L;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1