java button pressed for 3 seconds

前端 未结 5 659
太阳男子
太阳男子 2021-01-21 16:22

I created a button and I want these:

When the user clicks the button, it stays pressed like 3 seconds. After 3 seconds the button should be again look pressable. So the

5条回答
  •  长发绾君心
    2021-01-21 17:05

    btn.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
       btn.setEnabled(false);
       Timer timer = new Timer( 3000, new ActionListener(){
         @Override
         public void actionPerformed( ActionEvent e ){
           btn.setEnabled( true );
         }
       }
       timer.setRepeats( false );
       timer.start();
     }
    });
    

    I took the answer from @vels4j and added the javax.swing.Timer to it

提交回复
热议问题