1. 程式人生 > >監聽器接口(三)

監聽器接口(三)

log cnblogs pre this [] ext implement operation setbounds

//  通過接口實現監聽器
public class ActionMonitor extends JFrame implements ActionListener{
    JButton button;
    public ActionMonitor(){
        this.setLayout(null);//布局
        this.setSize(500,400);//大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置可以關閉
        button =new JButton();
        button.setBounds(
200, 200, 100, 20);//位置和大小 button.addActionListener(this);//添加監聽 this.add(button); } @Override public void actionPerformed(ActionEvent e) { // 監聽動作 if(e.getSource()==button){ } } public static void main(String[] args) { ActionMonitor am= new ActionMonitor(); } }

監聽器接口(三)