连续两次单击JButton [英] Clicking a JButton twice in a row
问题描述
我想知道是否在Java中有一个内置函数,或者是一种检查JButton是否连续单击两次的方法.请勿将其与跟踪JButton被单击或双击JButton的次数混淆.我搜索了多个线程,他们似乎都在谈论后者.
I was wondering if there was a built in function in java or a way to check if a JButton is clicked twice in a row. This is not to be confused with keeping track of how many times a JButton has been clicked or double clicking a JButton. I have searched multiple threads and they all seem to be talking about the latter.
如果单击一次名为button1
的按钮,则我可以使用以下代码.我需要一些帮助来调整它的功能,以便在两次单击按钮时(即背对背)单击该按钮.
I have the following code that works if a button called button1
is clicked once. I need a bit of help tweaking it to work for when the button is clicked twice i.e, back to back.
public void actionPerformed(ActionEvent arg0){
JButton button1 = (JButton) arg0.getSource();
if (button1 == button1) {
//You clicked button1 twice in a row
}
}
推荐答案
更多上下文会很好,但是您可以轻松地找到一种跟踪已按下的按钮以存储所需信息的方法(最后按下按钮的实例,一个计数器按下了多少次,或包含最后一个事件的数组...).像这样:
More context would be nice, but you can easily make a way to track what buttons has been pressed storing the information you need (last button pressed instance, a counter of how many times was pressed, or an array with the last events...). Something like:
private JButton _lastButtonPressed;
void actionPerformed(ActionEvent event)
{
JButton buttonPressed = (JButton) event.getSource();
if (_lastButtonPressed == buttonPressed)
{
//The same button was clicked two+ times in a row
}
else
{
//code for handling single button presses
}
_lastButtonPressed = buttonPressed;
}
更新
这是一个简单的可执行程序,它利用了上面的代码. actionListener可以知道连续按下Button的次数.
Here's a simple executable program that makes use of the code above. The actionListener is able to know how many times in a row a Button has been pressed.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class ButtonTestFrame implements ActionListener
{
public static final void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ButtonTestFrame();
}
});
}
public ButtonTestFrame()
{
_frame = new JFrame();
_frame.setTitle("Button test frame");
_textArea = new JTextArea("Press some buttons:\n");
_textArea.setEditable(false);
_frame.add(new JScrollPane(_textArea));
JPanel buttonsPanel = new JPanel(new GridLayout(1, 3, 1, 1));
_buttonA = new JButton("Button A");
_buttonA.addActionListener(this);
_buttonB = new JButton("Button B");
_buttonB.addActionListener(this);
_buttonC = new JButton("Button C");
_buttonC.addActionListener(this);
buttonsPanel.add(_buttonA);
buttonsPanel.add(_buttonB);
buttonsPanel.add(_buttonC);
_frame.add(buttonsPanel, BorderLayout.SOUTH);
_frame.setPreferredSize(new Dimension(600,400));
_frame.pack();
_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
_frame.setLocationByPlatform(true);
_frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JButton buttonPressed = (JButton) event.getSource();
if (_lastButtonPressed == buttonPressed)
{
//The same button was clicked two+ times in a row
count++;
_textArea.append(buttonPressed.getText() + " has been pressed " + count + " times in a row.\n");
}
else
{
//code for handling single button presses
count = 1;
_textArea.append(buttonPressed.getText() + " has been pressed.\n");
}
_lastButtonPressed = buttonPressed;
}
private int count = 0;
private JButton _lastButtonPressed;
private JButton _buttonA;
private JButton _buttonB;
private JButton _buttonC;
private JTextArea _textArea;
private JFrame _frame;
}
结果:
这篇关于连续两次单击JButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!