首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

【Java温习】第五讲 AWT图形用户界面设计

2012-11-01 
【Java复习】第五讲 AWT图形用户界面设计5.1.1 java.awt包?? ?  在程序中安排组件的位置和大小时,应该注意以

【Java复习】第五讲 AWT图形用户界面设计

5.1.1 java.awt包

?? ?  在程序中安排组件的位置和大小时,应该注意以下两点:
  1.容器中的布局管理器负责各个组件的大小和位置,因此用户无法在这种情况下设置组件的这些属性。如果试图使用Java 语言提供的setLocation(),setSize(),setBounds() 等方法,则都会被布局管理器覆盖。

AWT的组件类中提供注册和注销监听器的方法:
  ◇ 注册监听器:
  public void add<ListenerType> (<ListenerType>listener);
  ◇ 注销监听器:
  public void remove<ListenerType> (<ListenerType>listener);

  例如Button类:(查API)
  public class Button extends Component {
     ……
     public synchronized void addActionListener(ActionListener l);
     public synchronized void removeActionListener(ActionListener l);
     ……}

事件类别

描述信息

接口名

方法

 ActionEvent

激活组件

  ActionListener

 actionPerformed(ActionEvent)

 ItemEvent

选择了某些项目

  ItemListener

 itemStateChanged(ItemEvent)

 MouseEvent

鼠标移动

  MouseMotionListener

 mouseDragged(MouseEvent)
 mouseMoved(MouseEvent)

鼠标点击等

  MouseListener

 mousePressed(MouseEvent)
 mouseReleased(MouseEvent)
 mouseEntered(MouseEvent)
 mouseExited(MouseEvent)
 mouseClicked(MouseEvent)

 KeyEvent

键盘输入

  KeyListener

 keyPressed(KeyEvent)
 keyReleased(KeyEvent)
 keyTyped(KeyEvent)

 FocusEvent

组件收到或失去焦点

  FocusListener

 focusGained(FocusEvent)
 focusLost(FocusEvent)

 AdjustmentEvent

移动了滚动条等组件

  AdjustmentListener

 adjustmentValueChanged(AdjustmentEvent)

 ComponentEvent

对象移动缩放显示隐藏等

  ComponentListener

 componentMoved(ComponentEvent)
 componentHidden(ComponentEvent)
 componentResized(ComponentEvent)
 componentShown(ComponentEvent)

 WindowEvent

窗口收到窗口级事件

  WindowListener

 windowClosing(WindowEvent)
 windowOpened(WindowEvent)
 windowIconified(WindowEvent)
 windowDeiconified(WindowEvent)
 windowClosed(WindowEvent)
 windowActivated(WindowEvent)
 windowDeactivated(WindowEvent)

 ContainerEvent

容器中增加删除了组件

  ContainerListener

 componentAdded(ContainerEvent)
 componentRemoved(ContainerEvent)

 TextEvent

文本字段或文本区发生改变

  TextListener

 textValueChanged(TextEvent)

? f.addMouseMotionListener(this); //注册监听器MouseMotionListener
 f.addMouseListener(this); //注册监听器MouseListener
 f.addWindowListener(this); //注册监听器WindowListener     class MyMouseMotionListener extends MouseMotionAdapter{ /*内部类开始*/
       public void mouseDragged(MouseEvent e) {
         String s="Mouse dragging: x="+e.getX()+"Y="+e.getY();
         tf.setText(s); }
       } ;

       public static void main(String args[]) {
         InnerClass obj=new InnerClass();
         obj.launchFrame();
       }
     }//内部类结束
    }

 监听器接口

Act

Adj

Cmp

Cnt

Foc

Itm

Key

Mou

MM

Text

Win

  Button

?

?

?

?

?

  Canvas

?

?

?

?

?

?

  Checkbox

?

?

?

?

?

CheckboxMenuItem

?

?

?

?

?

?

?

?

?

?

  Choice

?

?

?

?

?

  Component

?

?

?

?

?

?

  Container

?

?

?

?

?

  Dialog

?

?

?

?

  Frame

?

?

?

?

  Label

?

?

?

?

?

?

  List 

?

?

?

?

  MenuItem

?

?

?

?

?

?

?

?

?

?

  Panel

?

?

?

?

?

  Scrollbar

?

?

?

?

?

  ScrollPane

?

?

?

?

?

  TextArea

?

?

?

?

?

  TextField

?

?

?

?

  Window

?

?

?

?


  Act=ActionListener Adj=AdjustmentListener Cmp=ComponentListener
  Cnt=ConatainerListener Foc=FocusListener Itm=ItemListener
  Key=KeyListener Mou=MouseListener MM=MouseMotionListener
  Text=TextListener Win=WindowListener

【本讲小结】
  用AWT来生成图形化用户界面时,组件和容器的概念非常重要。组件是各种各样的类,封装了图形系统的许多最小单位,例如按钮、窗口等等;而容器也是组件,它的最主要的作用是装载其它组件,但是象Panel这样的容器也经常被当作组件添加到其它容器中,以便完成杂的界面设计。布局管理器是java语言与其它编程语言在图形系统方面较为显著的区别,容器中各个组件的位置是由布局管理器来决定的,共有5种布局管理器,每种布局管理器都有自己的放置规律。事件处理机制能够让图形界面响应用户的操作,主要涉及到事件源、事件、事件处理者等三方,事件源就是图形界面上的组件,事件就是对用户操作的描述,而事件处理者是处理事件的类。因此,对于AWT中所提供的各个组件,我们都需要了解该组件经常发生的事件以及处理该事件的相应的监听器接口。