日期:2014-05-20 浏览次数:20752 次
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.*; import javax.swing.*; class ButtonActionFrame extends JFrame { ButtonActionFrame() { super("按钮响应"); setSize(250,200); setLocation(400,300); JButton jb=new JButton("OK"); JPanel jp=new JPanel(); jp.add(jb); add(jp); ButtonListener bla=new ButtonListener(); //创建监听对象 jp.addActionListener(bla);//注册监听对象 } } class ButtonListener implements ActionListener //定义监听类 { public void actionPerformed(ActionEvent e) { System.out.println("你单击了按钮!"); } } public class Example7_6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JFrame f=new ButtonActionFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }