BangBean 繼承 Canvas,實作 Serializable,包含:
ActionListener:addActionListener/removeActionListener 拋出 TooManyListenersException。MouseAdapter 繪製圓形跟隨游標;MouseMotionAdapter 更新座標並重繪。實作 Serializable 讓建構工具持久化屬性值,運行時自動還原。
滑鼠按下時在中心繪製文字,並通知 ActionListener:
if (actionListener != null) {
actionListener.actionPerformed(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
}
覆寫 paint() 繪製黑框圓形;getPreferredSize() 返回建議尺寸 200x200。
內建 main() 建立 Frame 嵌入 Bean,註冊列印動作的監聽器,方便獨立驗證。