读书人

Java学习二-17 事件

发布时间: 2013-11-08 17:52:35 作者: rapoo

Java学习2-17 事件

? ?确定事件->事件监听->绑定

? ?ActionListener,MouseListener,KeyListener,WindowListener

? ?eg:简单的button按钮触发事件导致窗体改变颜色

? ? ? ? 可以用内部类来实现事件,再绑定

? ? ? ?1.在构造器里面设置。this.setLayout(new FlowLayout()

? ? ? ?2.添加按钮。 ? ? ? ? ? ? jb_Red = new JButton("Red");

? ? ? ?3.把按钮添加到窗体。this.getContentPane().add(jb_Red);

? ? ? ?4. 事件

? ? ? ? ? ?if (e.getActionCommand().equals("Red")) {

? ? ActionLis.this.getContentPane().setBackground(Color.red);}

? ? ? ?5.绑定 。 ? ? ? ? ? jb_Red.addActionListener(new ActionListenerimpl());

?

? ?事件重在理解,应该多加练习。周末把前面的课程再练习几次。

?

?

读书人网 >其他相关

热点推荐