组件(Conponent)是图形用户界面最基本的部分,也称为构件 ,是可以以图形化的方式显示在屏幕上,并能与用户进行交互的对象,例如一个按钮,一个标签等。
组件不能独立地显示出来,必须将其放在一定的容器中才可以显示。
类java.awt.Component是许多组件类的父类,Component类中封装了组件通用的方法和属性,如图形的组件对象、大小、显示位置、前景色和背景色、边界、可见性等。
1 package TomAwt; 2 3 4 import java.applet.Applet; 5 import java.awt.*; 6 import java.awt.event.*; 7 public class TomAwt_12 extends Frame implements WindowListener{ 8 public TomAwt_12(){ 9 super("可关闭的窗口"); 10 setSize(300,200); 11 setVisible(true); 12 //add WindowListener to this frame 13 addWindowListener(this); 14 } 15 //implementation of WindowListener 16 public void windowOpened(WindowEvent e){} 17 public void windowClosed(WindowEvent e){} 18 public void windowIconified(WindowEvent e){} 19 public void windowDeiconified(WindowEvent e){} 20 public void windowActivated(WindowEvent e){} 21 public void windowDeactivated(WindowEvent e){} 22 public void windowClosing(WindowEvent e){ 23 System.exit(0); //return to OS 24 } 25 public static void main(String[] args){ 26 new TomAwt_12(); 27 } 28 }