早期 AWT 事件模型(Java 1.0/1.1)示例:Applet 實現 action(Event, Object) 方法處理按鈕點擊,修改座標變量觸發 repaint(),paint(Graphics) 繪製移動字符串。代碼展示舊事件模型與 Graphics 繪圖基礎。
public class AppletEvent extends Applet {
int x = 100, y = 40;
Button b = new Button("你就按著玩兒吧!");
public AppletEvent() { add("Center", b); }
public void paint(Graphics g) {
g.setColor(Color.red);
g.setFont(new Font("Helvetica", Font.PLAIN, 24));
g.drawString("InofCD歡迎您!", x, y);
}
public boolean action(Event ev, Object arg) {
if (ev.target instanceof Button) {
y += 10; x -= 10;
if (y >= 250) y = 10;
if (x <= 0) x = 100;
repaint();
return true;
}
return false;
}
}
action(Event, Object),事件傳播層層向上。Event 封裝源組件、按鍵、座標等;arg 為按鈕標簽等附加信息。true 表示已處理,阻止繼續傳播。button.addActionListener(e -> {
y += 10; x -= 10;
// 邊界檢查...
repaint();
});
ActionListener、MouseListener 等)。舊模型已過時(Java 1.1 棄用),僅供維護極老代碼或教學參考。