7. java lecture events
- 1. 弍舒弍仂从舒 仂弍亳亶
弌仂弍亳亠 仂弍亠从 仗亠亳舒仍仆仂亞仂 从仍舒舒,
仂仗亳于舒ム亳亶 亳亰仄亠仆亠仆亳亠 仂仂礌亳
仂弍亠从舒 亳仂仆亳从舒.
仍舒 亟仍 仂弍舒弍仂从亳 仂弍亳亶
仆舒仂亟 于 仗舒从亠亠 java.awt.event
1
- 3. 弍舒弍仂从舒 仂弍亳亶
仍舒 仂弍亳亶
亠仂亟 从仍舒舒 EventObject:
- Object getSource() 仍从舒 仆舒 仂弍亠从亳仂仆亳从 仂弍亳
- String toString() 亠从仂于仂亠 仂仗亳舒仆亳亠
仂弍亳
3
- 4. 弍舒弍仂从舒 仂弍亳亶
仂仆亳从亳 仂弍亳亶
仂仆亳从
弌仂弍亳亠
仂亞亟舒 于仂亰仆亳从舒亠
JButton
ActionEvent
舒亢舒舒 从仆仂仗从舒
JCheckBox
ItemEvent
弌弍仂/舒仆仂于从舒 仍舒亢从舒
JRadioButton
ItemEvent
弌弍仂/舒仆仂于从舒 仍舒亢从舒
JComboBox
ActionEvent
ItemEvent
弍仂 仍亠仄亠仆舒 仗亳从舒
亰仄亠仆亠仆亳亠 仂仂礌亳 仍亠仄亠仆舒
JList
ListSelection
Event
亰仄亠仆亠仆亳亠 仂仂礌亳 仍亠仄亠仆舒
JMenuItem
ActionEvent
ItemEvent
亰仄亠仆亳仍仂 仂-亠 仗仆从舒 仄亠仆
丐仂 亢亠 亟仍 仗仆从舒 仄亠从仂亶
4
- 5. 弍舒弍仂从舒 仂弍亳亶
仂仆亳从亳 仂弍亳亶
仂仆亳从
弌仂弍亳亠
仂亞亟舒 于仂亰仆亳从舒亠
JScrollbar
AdjustmentEvent
舒仆亳仗仍亳亳 仗仂仍仂舒仄亳
仗仂从从亳
JTextField
JTextArea
TextEvent
于仂亟 亳仄于仂仍仂于
JFrame
JWindow
Jdialog
WindowEvent
ミ英亠 亟亠亶于亳 仂从仆仂仄
(仂从/亰舒从,
于亠仆/舒亰于亠仆 亳 .仗.)
亠 仂亞舒仆
仗舒于仍亠仆亳
FocusEvent
仂仄仗仂仆亠仆 仗仂仍舒亠 亳仍亳 亠磳
仂从 于于仂亟舒
亠
ContainerEvent
从仂仆亠亶仆亠
仂仄仗仂仆亠仆 亟仂弍舒于仍磳 亳仍亳
亟舒仍磳 亳亰 从仂仆亠亶仆亠舒
5
- 7. 弍舒弍仂从舒 仂弍亳亶
仆亠亠亶 - 仍舒亠仍亳
仍 从舒亢亟仂亞仂 亳仗舒 仂弍亳亶 亠于亠 于仂亶 亳仆亠亠亶仍舒亠仍:
ActionEvent ActionListener
ItemEvent ItemListener
WindowEvent WindowListener
亳 .亟.
亳仆亠亠亶亠-仍舒亠仍亠 仂仗亳舒仆 仄亠仂亟,
仂弍舒弍舒于舒ム亳亠 仂弍亳 亟舒仆仆仂亞仂 从仍舒舒.
7
- 8. 弍舒弍仂从舒 仂弍亳亶
仍仂从亳 仗仂仍亳于舒仆亳
仍仂从 仗仂仍亳于舒仆亳 从仍舒, 亠舒仍亳亰ム亳亶
亳仆亠亠亶-仍舒亠仍 (亠舒仍亳亰ム亳亶 于亠 亠亞仂
仄亠仂亟).
仍 仂弍舒弍仂从亳 仂弍亳 仆亠仂弍仂亟亳仄仂:
1. 弌仂亰亟舒 弍仍仂从 仗仂仍亳于舒仆亳
2. 弌于磶舒 弍仍仂从 仗仂仍亳于舒仆亳 亳仂仆亳从仂仄 仂弍亳
8
- 9. 弍舒弍仂从舒 仂弍亳亶
仍仂从亳 仗仂仍亳于舒仆亳
丼仂弍 于磶舒 弍仍仂从仂仄 仗仂仍亳于舒仆亳, 从仍舒-亳仂仆亳从亳
仂弍亳 亟仂仍亢仆 仗仂仍亳 仍从 仆舒 从亰亠仄仗仍 弍仍仂从舒
仗仂仍亳于舒仆亳 (从仍舒舒-仂弍舒弍仂亳从舒 仂弍亳)
亰舒亠亞亳亳仂于舒 弍仍仂从 仗仂仍亳于舒仆亳:
public void addTypeListener(TypeListener obj)
亞亟亠 Type 仂 亳仄 仂弍亳,
obj 仍从舒 仆舒 从亰亠仄仗仍 从仍舒舒-仂弍舒弍仂亳从舒 仂弍亳.
亠仂亟, 从仂仂亠 亠亞亳亳ム 弍仍仂从 仗仂仍亳于舒仆亳,
仂弍亠仗亠亳于舒ム 亞亠仆亠亳ム亳仄 仂弍亳亠 亳仂仆亳从仂仄.
9
- 12. 弍舒弍仂从舒 仂弍亳亶
仍仂从亳 仗仂仍亳于舒仆亳: 仆亠从仂仍从仂
亳仂仆亳从仂于
仂仆亳从
仂弍亳
仂仆亳从
仂弍亳
仂仆亳从
仂弍亳
addTypeListener(TypeListener obj)
仍仂从
仗仂仍亳于舒仆亳
addTypeListener(TypeListener obj)
12
- 13. 弍舒弍仂从舒 仂弍亳亶
弌 仗仂仄仂 仂亟亠仍仆仂亞仂 从仍舒舒
class TextMove implements ActionListener
{
private JTextField jtf;
TextMove(JTextField jtf)
{ this.jtf = jtf; }
public void actionPerformed(ActionEvent obj)
{ jtf.setText("Button pressed"); }
}
13
- 14. 弍舒弍仂从舒 仂弍亳亶
弌 仗仂仄仂 仂亟亠仍仆仂亞仂 从仍舒舒
class MyNotebook extends JFrame
{public static void main(String args[ ])
{JFrame jf = new JFrame(弍舒弍仂从舒 仂弍亳 ActionEvent");
Container cp = jf.getContentPane();
jf.setLayout(new FlowLayout());
JButton jb = new JButton("OK"); JTextField jtf = new JTextField(20);
cp.add(jb);cp.add(jtf);
jb.addActionListener(new TextMove(jtf));
jf.setVisible(true); }
}
14
- 16. 弍舒弍仂从舒 仂弍亳亶
仆亳 从仍舒舒-亳仂仆亳从舒
class MyNotebook extends JFrame implements ActionListener
{private JTextField jtf;
public static void main(String args[ ])
{JFrame jf = new JFrame(弍舒弍仂从舒 仂弍亳 ActionEvent");
Container cp = jf.getContentPane();
jf.setLayout(new FlowLayout());
JButton jb = new JButton("OK");
JTextField jtf = new JTextField(20);
16
- 18. 弍舒弍仂从舒 仂弍亳亶
弌 仗仂仄仂 于仍仂亢亠仆仆仂亞仂 从仍舒舒
class MyNotebook extends JFrame
{private JTextField jtf;
public static void main(String args[ ])
{JFrame jf = new JFrame(弍舒弍仂从舒 仂弍亳 ActionEvent");
Container cp = jf.getContentPane();
jf.setLayout(new FlowLayout());
JButton jb = new JButton("OK");
JTextField jtf = new JTextField(20);
18
- 19. 弍舒弍仂从舒 仂弍亳亶
弌 仗仂仄仂 于仍仂亢亠仆仆仂亞仂 从仍舒舒
cp.add(jb);
cp.add(jtf);
jb.addActionListener(new TextMove());
jf.setVisible(true);
}
class TextMove implements ActionListener
{public void actionPerformed(ActionEvent obj)
{ jtf.setText("Button pressed"); }
}
19
- 20. 弍舒弍仂从舒 仂弍亳亶
弌 仗仂仄仂 舒仆仂仆亳仄仆仂亞仂
于仍仂亢亠仆仆仂亞仂 从仍舒舒
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent obj)
{ jtf.setText("Button pressed"); }
} );
20
- 21. 弍舒弍仂从舒 仂弍亳亶
亠仂亟 亳仆亠亠亶舒 ActionListener
void actionPerformed(ActionEvent obj)
亠仂亟 亳仆亠亠亶舒 AdjustmentListener
void adjustmentValueChanged(AdjustmentEvent obj)
21
- 22. 弍舒弍仂从舒 仂弍亳亶
亠仂亟 亳仆亠亠亶舒 ComponentListener
void ComponentResized(ComponentEvent obj)
void ComponentMoved(ComponentEvent obj)
void ComponentShown(ComponentEvent obj)
void ComponentHidden(ComponentEvent obj)
亠仂亟 亳仆亠亠亶舒 ContainerListener
void componentAdded(ContainerEvent obj)
void componentRemoved(ContainerEvent obj)
22
- 23. 弍舒弍仂从舒 仂弍亳亶
亠仂亟 亳仆亠亠亶舒 FocusListener
void focusGained(FocusEvent obj)
void focusLost(FocusEvent obj)
亠仂亟 亳仆亠亠亶舒 ItemListener
void ItemStateChanged(ItemEvent obj)
亠仂亟 亳仆亠亠亶舒 KeyListener
public void keyTyped(KeyEvent obj);
public void keyPressed(KeyEvent obj);
public void keyReleased(KeyEvent obj);
23
- 24. 弍舒弍仂从舒 仂弍亳亶
亠仂亟 亳仆亠亠亶舒 MouseListener
public void mouseClicked(MouseEvent obj);
public void mousePressed(MouseEvent obj);
public void mouseReleased(MouseEvent obj);
public void mouseEntered(MouseEvent obj);
public void mouseExited(MouseEvent obj);
亠仂亟 亳仆亠亠亶舒 MouseMotionListener
public void mouseDragged(MouseEvent obj);
public void mouseMoved(MouseEvent obj);
24
- 25. 弍舒弍仂从舒 仂弍亳亶
亠仂亟 亳仆亠亠亶舒 TextListener
void textValueChanged(TextEvent obj)
亠仂亟 亳仆亠亠亶舒 WindowListener
public void windowOpened(WindowEvent obj);
public void windowClosed(WindowEvent obj);
public void windowClosing(WindowEvent obj);
public void windowActivated(WindowEvent obj);
public void windowDeactivated(WindowEvent
obj);
public void windowIconified(WindowEvent obj);
public void windowDeiconified(WindowEvent obj);
25
- 26. 弍舒弍仂从舒 仂弍亳亶
仍舒-舒亟舒仗亠
仍 亠舒仍亳亰舒亳亳 亳仆亠亠亶舒-仍舒亠仍 仆亠仂弍仂亟亳仄仂 仂仗亠亟亠仍亳
于亠 亠亞仂 仄亠仂亟. 仍亳 仆亠仂弍仂亟亳仄仂 仂弍舒弍舒于舒 于 仗仂亞舒仄仄亠
仂仍从仂 仆亠从仂仂亠 亳亰 仂弍亳亶, 仂弍舒弍仂从舒 从仂仂
仗亠亟仄舒亳于舒亠 从仂仆从亠仆仄 亳仆亠亠亶仂仄-仍舒亠仍亠仄, 舒 于亠
仗仂亳亠 亳亞仆仂亳仂于舒, 仂 仄仂亢仆仂 于仂仗仂仍亰仂于舒 仗亠亳舒仍仆仄
从仍舒舒仄亳, 从仂仂亠 仆舒亰于舒ム 从仍舒舒仄亳-舒亟舒仗亠舒仄亳. 仍舒舒亟舒仗亠 仂弍亠仗亠亳于舒ム 仗 亠舒仍亳亰舒亳 于亠 仄亠仂亟仂于
亳仆亠亠亶舒, 仂 亟舒亠 于仂亰仄仂亢仆仂 仆舒仍亠亟仂于舒 从仍舒仂弍舒弍仂亳从 仂弍亳 仂 从仍舒舒-舒亟舒仗亠舒 亳 亠舒仍亳亰仂于于舒 于 仆亠仄
仆亠 于亠 仄亠仂亟 亳仆亠亠亶舒, 舒 仍亳 亠 从仂仂亠 仆亠仂弍仂亟亳仄.
26