连续两次单击JButton

时间:2014-02-01 10:53:33

标签: java user-interface jbutton

我想知道java中是否有内置函数或者检查是否连续两次点击JButton。这不应与跟踪JButton被点击或双击JButton的次数相混淆。我搜索了多个主题,他们似乎都在谈论后者。

如果单击一次名为button1的按钮,我有以下代码。我需要一些帮助来调整它,以便在按钮被点击两次时工作,即背靠背。

public void actionPerformed(ActionEvent arg0){
    JButton button1 = (JButton) arg0.getSource();
    if (button1 == button1) {
        //You clicked button1 twice in a row
    }
}

1 个答案:

答案 0 :(得分:2)

更多上下文会很好,但您可以轻松地找到一种方法来跟踪按下哪些按钮存储您需要的信息(按下最后一个按钮的实例,按下了多少次的计数器,或者包含最后一个事件的数组) ...)。类似的东西:

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能够知道按下按钮的次数。

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;
}

结果:

Button test frame