JAVA 学习笔记 - AWT
仅限个人学习笔记,有问题请大神指出。
AWT为程序提供用户界面,可用于FORM窗口编程。程序包中提供了容器,比如窗口JFrame,Panle,SorollPanle,Dialog,FileDialog等,以及各种组件(或控件),比如Button,TextField,Checkbox,CheckboxGroup,Choice(选项),Label,LIst,TextArea等,辅助控件:Scrollbar等,布局控制器:Flowlayout,BorderLayout,GridLayout,GridBagLayout,CardLayout(这种部件类似选项卡页),BoxLayout,当然还有绝对布局,以及Canvas画布、Menubar\Menu\MenuItem。
所有容器、控件所在包:java.awt.*
具体类的使用,可在API中查到。
只进行界面布局完全不能进行业务逻辑处理,比如点按钮执行操作什么,还必须引入一个东西:事件处理!。
怎么让控件挂接事件,利用增加监听,如下。添加有几种方式,外部监听类、内部监听类、内部匿名监听类、自己继承监听接口或类。
Frame f = new Frame("Click me");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
上述WindowAdapter其实是实现了WindowEvent接口的空实现类。其实也可以定义一个外部类进行绑定,如下:
class CLOSEListener implementsWindowEvent
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
f.addWindowListener(new CLOSEListener());
其实作为控件事件,作为人机交互必要元素,事件种类有很多种。
事件、监听和处理器之间的关系。
几者之间的关系: new Frame().addXXXListener(new XXXAdapter(){
public 时机(XXXEvent e){
...;
}
})
单独提下画图相关的方法,在Component类里提供了几个与绘图有关的方法。
paint(Graphics g);
update(Graphics g);
repaint();