有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何使用处理程序根据java中的按钮点击次数输出不同的文本

我正在尝试使用处理程序来完成一些基于按钮点击的任务。例如,如果按钮按下一次,则必须输出1;如果按钮按下两次,则必须输出2。 这必须在5秒内完成。我用安卓系统做过。但是我在用java做这件事时遇到了一些错误。这是等效的安卓代码。我只想使用处理程序在java中实现同样的功能

@Override
public void onBackPressed() {
    if (dblBckToExitPrssdOnce) {
        super.onBackPressed();
        return;
    }

    this.dblBckToExitPrssdOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            dblBckToExitPrssdOnce=false;                       
        }
    }, 5000);
} 

共 (1) 个答案

  1. # 1 楼答案

    只需使用一个标志isFirstClick。如果是(第一次点击),启动计时器(^{)。如果计时器在第二次单击之前熄灭,请执行一些操作,例如禁用按钮。如果及时按下按钮,停止计时器。例如:

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class TimedButtonClickDemo {
    
        private JButton button = null;
    
        public TimedButtonClickDemo() {
            button = getTimedButton();
            JFrame frame = new JFrame();
            frame.add(button);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
    
            frame.setVisible(true);
        }
    
        public JButton getTimedButton() {
            JButton button = new JButton("Press Me");
            button.addActionListener(new TimerButtonListener());
            return button;
        }
    
        private class TimerButtonListener implements ActionListener {
    
            private Timer timer = null;
            private boolean isFirstClick = true;
    
            public TimerButtonListener() {
                timer = new Timer(5000, new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        button.setEnabled(false);
                        System.out.println("Too slow, Jack! Better eat your Wheaties!");
                    }
                });
                timer.setRepeats(false);
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isFirstClick) {
                    button.setText("Press Again");
                    timer.start();
                    isFirstClick = false;
                } else {
                    timer.stop();
                    button.setText(" Press Me ");
                    isFirstClick = true;
                    System.out.println("You did it. Must be on that Red Bull");
                }   
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new TimedButtonClickDemo();
                }
            });
        }
    }
    

    有关如何使用计时器的更多信息,请访问How to Use Swing Timers