事件监听
public class demo06 { public static void main(String[] args) { Frame frame = new Frame(); Button button = new Button("hahahhaha"); frame.add(button); frame.setVisible(true); frame.setSize(100,100); button.addActionListener(new MyActionListener()); windowClose(frame); } public static void windowClose(Frame frame){ frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } } class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println("hahahahahahahahah"); }
}
为按钮添加监听事件,构造一个内部类实现ActionListener接口,重写里面的方法

关闭窗体的方法,将窗体对象作为参数,这样新建一个船体的时候就不需要重新添加窗体监听器了,只需将其作为参数执行这个方法

浙公网安备 33010602011771号