顯示具有 event handler 標籤的文章。 顯示所有文章
顯示具有 event handler 標籤的文章。 顯示所有文章

Two ways to write action event handlers in JavaFX

在 javafx.scene.control 套件路徑下,如下元件受點選都會產生動作事件(ActionEvent),

    Button/TextField/CheckBox/ComboBox/RadioButton/MenuItem/Hyperlink

可對元件註冊動作事件處理器(ActionEvent Handler),接收動作事件,進行處理。

註冊動作事件處理器有兩種寫法:

1.利用元件的.setOnAction方法註冊,寫法如下

button.setOnAction(event -> System.out.println("Button clicked!"));button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Button clicked!");
    }
});


2. 利用.fxml檔的元件屬性註冊,寫法如下

<Button text="Click Me" onAction="#handleButtonAction"/>

public class Controller {

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("Button clicked!");
    }
}

註: 方法前的 @ 標註旨在通知編譯器作防呆檢查,減少可能錯誤
1. @Override 標註將提醒編譯器檢查該方法簽名是否有覆蓋上一代方法
2. @FXML 標註將提醒編譯器檢查該方法或屬性是否出現於 .fxml 介面配置檔中

5 kinds of event handler styles

依處理器所掛的位置,總共有5種寫法,以計時器處理器為例,
A.處理器掛在外部類別下:

 import javax.swing.Timer;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 public class TimerTest1
 {
   public static void main(String args[]) throws Exception
   {
     ActionListener al = new TimerHandler();
     Timer t = new Timer(1000,al);
     t.start();
     Thread.sleep(10000);
     t.stop();
   }
 }
 
class TimerHandler implements ActionListener
 {
   public void actionPerformed(ActionEvent ae)
   {
     System.out.println("執行每次叫醒要作的事1");
   }
 }


B.處理器掛在內部匿名類別下:

 import javax.swing.Timer;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 public class TimerTest2
 {
   public static void main(String args[]) throws Exception
   {
     ActionListener al = new ActionListener()
       {
  public void actionPerformed(ActionEvent ae)
  {
    System.out.println("執行每次叫醒要作的事2");
  }
       };
     Timer t = new Timer(1000,al);
     t.start();
     Thread.sleep(10000);
     t.stop();
   }
 }


C.處理器掛在內部有名類別下:

 import javax.swing.Timer;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 public class TimerTest3
 {
   public static void main(String args[]) throws Exception
   {
     new TimerTest3();
   }

   TimerTest3() throws Exception
   {
     ActionListener al = new TimerHandler();
     Timer t = new Timer(1000,al);
     t.start();
     Thread.sleep(10000);
     t.stop();
   }

   private class TimerHandler implements ActionListener
   {
     public void actionPerformed(ActionEvent ae)
     {
       System.out.println("執行每次叫醒要作的事3");
     }
   }
 }


D.處理器掛在本身類別下:

 import javax.swing.Timer;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 public class TimerTest4 implements ActionListener
 {
   public void actionPerformed(ActionEvent ae)
   {
     System.out.println("執行每次叫醒要作的事4");
   }

   public static void main(String args[]) throws Exception
   {
     ActionListener al = new TimerTest4();
     Timer t = new Timer(1000,al);
     t.start();
     Thread.sleep(10000);
     t.stop();
   }
 }


E.處理器同時掛在內部和外部類別下:

 import javax.swing.Timer;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 public class TimerTest5
 {
   public static void main(String args[]) throws Exception
   {
     ActionListener al = new ActionAdapter()
       {
  public void actionPerformed(ActionEvent ae)
  {
    System.out.println("執行每次叫醒要作的事5x");
  }
       };
     Timer t = new Timer(1000,al);
     t.start();
     Thread.sleep(10000);
     t.stop();
   }
 }

 class ActionAdapter implements ActionListener
 {
   public void actionPerformed(ActionEvent ae)
   {
     System.out.println("執行每次叫醒要作的事5");
   }
 }

Disable UDP to solve frequent RDP disconnection

解決 RDP 遠端桌面經常斷線問題:「關閉 UDP 傳輸」 在使用 Windows 內建的 遠端桌面(RDP, Remote Desktop Protocol) 連線時,你是否也常遇到連線突然卡死、畫面凍結,或是頻繁跳出「連線已中...

總網頁瀏覽量