java button pressed for 3 seconds

前端 未结 5 637
太阳男子
太阳男子 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 16:59

    You need to spawn a new thread when the button is clicked.

     button3.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent event) {
            try {
                Thread t = new Thread(){
                  int count = 0;
                  setPressedIcon();
                  while(count < 4){
                     try{
                      Thread.sleep(1000);
                      count++;
                     }
                  }
                  setUnpressedIcon();
                }
                t.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        setPressedIcon();///??
        public void widgetDefaultSelected(SelectionEvent event) {
        }
    });
    

    If you do it in the same thread as your UI, everything will be halted during the 3 seconds.

提交回复
热议问题