Java by API/java.awt/AWTEventMulticaster
Содержание
AWTEventMulticaster: add(ActionListener a, ActionListener b)
import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Key Text Sample");
KeyTextComponent keyTextComponent = new KeyTextComponent();
final JTextField textField = new JTextField();
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String keyText = actionEvent.getActionCommand();
textField.setText(keyText);
}
};
keyTextComponent.addActionListener(actionListener);
frame.add(keyTextComponent, BorderLayout.CENTER);
frame.add(textField, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class KeyTextComponent extends JPanel {
private ActionListener actionListenerList = null;
public KeyTextComponent() {
setBackground(Color.cyan);
KeyListener internalKeyListener = new KeyAdapter() {
public void keyPressed(KeyEvent keyEvent) {
if (actionListenerList != null) {
int keyCode = keyEvent.getKeyCode();
String keyText = KeyEvent.getKeyText(keyCode);
ActionEvent actionEvent = new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, keyText);
actionListenerList.actionPerformed(actionEvent);
}
}
};
MouseListener internalMouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
requestFocus();
}
};
addKeyListener(internalKeyListener);
addMouseListener(internalMouseListener);
}
public void addActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.add(actionListenerList,
actionListener);
}
public void removeActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.remove(actionListenerList,
actionListener);
}
}
AWTEventMulticaster: add(ItemListener a, ItemListener b)
// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski. Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.ruponent;
import java.awt.Frame;
import java.awt.ItemSelectable;
import java.awt.SystemColor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
class ItemEventComponent extends Component implements ItemSelectable {
boolean selected;
int i = 0;
ItemListener itemListener = null;
ItemEventComponent() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Object[] getSelectedObjects() {
Object o[] = new Object[1];
o[0] = new Integer(i);
return o;
}
public void addItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.add(itemListener, l);
}
public void removeItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.remove(itemListener, l);
}
public void processEvent(AWTEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
if (itemListener != null) {
selected = !selected;
i++;
itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED)));
}
}
}
}
public class MainClass extends Frame implements ItemListener {
MainClass() {
super("Listening In");
ItemEventComponent c = new ItemEventComponent();
add(c, "Center");
c.addItemListener(this);
c.setBackground(SystemColor.control);
setSize(200, 200);
}
public void itemStateChanged(ItemEvent e) {
Object[] o = e.getItemSelectable().getSelectedObjects();
Integer i = (Integer) o[0];
System.out.println(i);
}
public static void main(String args[]) {
MainClass f = new MainClass();
f.show();
}
}
AWTEventMulticaster: remove(ActionListener l, ActionListener oldl)
import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Key Text Sample");
KeyTextComponent keyTextComponent = new KeyTextComponent();
final JTextField textField = new JTextField();
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
String keyText = actionEvent.getActionCommand();
textField.setText(keyText);
}
};
keyTextComponent.addActionListener(actionListener);
frame.add(keyTextComponent, BorderLayout.CENTER);
frame.add(textField, BorderLayout.SOUTH);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class KeyTextComponent extends JPanel {
private ActionListener actionListenerList = null;
public KeyTextComponent() {
setBackground(Color.cyan);
KeyListener internalKeyListener = new KeyAdapter() {
public void keyPressed(KeyEvent keyEvent) {
if (actionListenerList != null) {
int keyCode = keyEvent.getKeyCode();
String keyText = KeyEvent.getKeyText(keyCode);
ActionEvent actionEvent = new ActionEvent(this,
ActionEvent.ACTION_PERFORMED, keyText);
actionListenerList.actionPerformed(actionEvent);
}
}
};
MouseListener internalMouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
requestFocus();
}
};
addKeyListener(internalKeyListener);
addMouseListener(internalMouseListener);
}
public void addActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.add(actionListenerList,
actionListener);
}
public void removeActionListener(ActionListener actionListener) {
actionListenerList = AWTEventMulticaster.remove(actionListenerList,
actionListener);
}
}
AWTEventMulticaster: remove(ItemListener l, ItemListener oldl)
// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski. Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.ruponent;
import java.awt.Frame;
import java.awt.ItemSelectable;
import java.awt.SystemColor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
class ItemEventComponent extends Component implements ItemSelectable {
boolean selected;
int i = 0;
ItemListener itemListener = null;
ItemEventComponent() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Object[] getSelectedObjects() {
Object o[] = new Object[1];
o[0] = new Integer(i);
return o;
}
public void addItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.add(itemListener, l);
}
public void removeItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.remove(itemListener, l);
}
public void processEvent(AWTEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
if (itemListener != null) {
selected = !selected;
i++;
itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED)));
}
}
}
}
public class MainClass extends Frame implements ItemListener {
MainClass() {
super("Listening In");
ItemEventComponent c = new ItemEventComponent();
add(c, "Center");
c.addItemListener(this);
c.setBackground(SystemColor.control);
setSize(200, 200);
}
public void itemStateChanged(ItemEvent e) {
Object[] o = e.getItemSelectable().getSelectedObjects();
Integer i = (Integer) o[0];
System.out.println(i);
}
public static void main(String args[]) {
MainClass f = new MainClass();
f.show();
}
}