1. There are objects in an event model: event source, event and listener
2. Event listening mechanism:
Event source Where the event occurs
Event What is going to happen
Event processing The solution for what happened
Event monitoring Associate the event source with the event
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class AddActionListener { public static void main(String[] args) { JFrame jf = new JFrame("AddActionListener"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 400, 300); JTextArea area=new JTextArea(20,10); area.setLineWrap(true); JButton jb=new JButton("秃头"); jb.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { area.setText("不要熬夜!"); } }); jf.add(area); jf.add(jb); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
##Example 2:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class AddActionListener { public static void main(String[] args) { JFrame jf = new JFrame("AddActionListener"); jf.setLayout(new FlowLayout(FlowLayout.LEFT)); jf.setBounds(400, 300, 400, 300); JTextArea area=new JTextArea(20,10); area.setLineWrap(true); JButton jb=new JButton("秃头"); jb.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { area.append("不要熬夜!"); } }); jf.add(area); jf.add(jb); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
is a Class, e
is an instance of that class. You can replace e
with whatever you like, for example. event
or object
The above is the detailed content of What is the JavaGUI event listening mechanism?. For more information, please follow other related articles on the PHP Chinese website!