📄 testaddlistener.java~16~
字号:
// 12-9 给 按 钮 添 加 监 听 器 。package AddListener;import java.awt.*;import java.awt.event.*;import java.awt.AWTEvent;public class TestAddListener implements ActionListener,WindowListener{ Frame f; Button b; public void create() { f=new Frame("testing"); b=new Button("press"); b.addActionListener(this);// 注 册 监 听 器 f.addWindowListener(this); f.add(b,"North"); f.setSize(200,200); f.setVisible(true); } public void actionPerformed(ActionEvent e){ System.out.println("the button is pressed"); } // 如果继承了接口 WindowListener ,下面 7 个方法都要写,否则出错! public void windowActivated(WindowEvent e){} public void windowClosed(WindowEvent e){} public void windowClosing(WindowEvent e){System.exit(1);} public void windowDeactivated(WindowEvent e){} public void windowDeiconified(WindowEvent e){} public void windowIconified(WindowEvent e){} public void windowOpened(WindowEvent e){} public static void main(String[] args) { TestAddListener test = new TestAddListener(); test.create(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -