资源预览内容
第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
亲,该文档总共8页全部预览完了,如果喜欢就下载吧!
资源描述
软件编程基础接口 形式:interface Stack void push (object x);object pop();格式:接口修饰字 interface 接口名称/方法的原形定义或静态常数事件处理软件编程基础使用:必须有implments关键字class A implment Stackvoid push (object x);/方法的程序代码object pop();/方法的程序代码 事件处理软件编程基础例 import java.awt.*; import java.applet.*; import java.awt.event.*; public class Grid extends Applet implements ActionListener public Color myColor; public Button btn1,btn2; public Label label1,label2; public TextField txt1; public TextArea txt2; public Panel p1,p2; 事件处理软件编程基础public void init( ) setLayout(new GridLayout(2,1,10,10);/*设置总体布局 为网格布局 btn1 = new Button(“RED“); btn1.setBackground(Color.red); btn1.setForeground(Color.yellow);btn2 = new Button(“GREEN“); btn2.setBackground(Color.green); btn2.setForeground(Color.blue);事件处理软件编程基础label1 = new Label(“TextField“); label2 = new Label(“TextArea“); txt1 = new TextField(10); txt2 = new TextArea(“,5,10); p1=new Panel(new GridLayout(1,3,10,10);/*改变为网格布局,一 行三列 p2 = new Panel(new GridLayout(3,1,10,10);/*改变为网格布局,三 行一列 add(p1); add(p2);事件处理软件编程基础p1.add(label1);p1.add(txt1);p1.add(btn1); p2.add(label2);p2.add(txt2);p2.add(btn2); btn1.addActionListener(this); btn2.addActionListener(this); public void paint(Graphics g) /画方法,图形颜色变 化 g.setColor(myColor); g.fillOval(60, 120, 30, 30);事件处理软件编程基础public void actionPerformed(ActionEvent e) /行为处理 if (e.getActionCommand().equals(“RED“) /情况1 myColor = Color.red; /改变颜色txt1.setText(“red“); /设置TextField中文本txt2.setText(“); /设置TextArea中文本 else if (e.getActionCommand().equals(“GREEN“) /情况2myColor = Color.green; txt1.setText(“); txt2.setText(“GREEN“); repaint(); /重画,必须有 事件处理软件编程基础在java.awt.event包中,有相应的类来表达事件,如 ActionEvent。在编程时需要通过事件监听器(Event Listener )来实现对事件的处理。事件监听器是一些事件的接口,这些接口是 java.awt.AWTEventListener的子类。接口中含有相关的方法, 如:ActionListener。Java中处理事件的基本方法,是事件监听器的注册,也就是 将组件对象与监听器对象相联系。注册事件监听器只需要使用组件对象的add*Event方法,它可以指明该对象感兴趣的事件监听器(即实现了某个 AWTEventListener子接口的对象)。这样,当事件源发生了某 种类型的事件时,则触发事先己注册过的监听器中相应的处理 程序。 事件处理
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号