书中本章主要是讨论可视化应用程序的制作,最开始所讨论的老式awt本身的设计十分糟糕,所以我直接跳过从13.16新型awt开始阅读,新型的awt给 出一套比老式awt完善且先进得多的消息机制,对此作者给出了这些消息的列表。虽然这些都可以在API中逐个查到,但是能整体的查看更能让读者对于新型 awt形成一个完整概念。
以下是书中列出的表格:
Event, listener interface and add- and remove-methods | Componentssupportingthisevent |
ActionEvent ActionListener addActionListener() removeActionListener() | Button,List,TextField,MenuItem,and its derivatives including CheckboxMenuItem,Menu,and PopupMenu |
AdjustmentEvent AdjustmentListener addAdjustmentListener() removeAdjustmentListener() | Scrollbar Anything you create that implements the Adjustable interface |
ComponentEvent ComponentListener addComponentListener() removeComponentListener() | Component and its derivatives, including Button,Canvas, Checkbox,Choice,Container,Panel,Applet,ScrollPane, Window,Dialog,FileDialog,Frame,Label,List,Scrollbar, TextArea,and TextField |
ContainerEvent ContainerListener addContainerListener() removeContainerListener() | Container and its derivatives, including Panel,Applet, ScrollPane,Window,Dialog,FileDialog,and Frame |
FocusEvent FocusListener addFocusListener() removeFocusListener() | Component and its derivatives, including Button,Canvas, Checkbox,Choice,Container,Panel,Applet,ScrollPane, Window,Dialog,FileDialog,FrameLabel,List,Scrollbar, TextArea,and TextField |
KeyEvent KeyListener addKeyListener() removeKeyListener() | Component and its derivatives, including Button,Canvas,Checkbox,Choice,Container,Panel,Applet,ScrollPane, Window,Dialog,FileDialog,Frame,Label,List,Scrollbar, TextArea,and TextField |
MouseEvent(for both clicks and motion) MouseListener addMouseListener() removeMouseListener() | Component and its derivatives, including Button,Canvas, Checkbox,Choice,Container,Panel,Applet,ScrollPane, Window,Dialog,FileDialog,Frame,Label,List,Scrollbar, TextArea,and TextField |
MouseEvent[55](for both clicks and motion) MouseMotionListener addMouseMotionListener() removeMouseMotionListener() | Component and its derivatives, including Button,Canvas, Checkbox,Choice,Container,Panel,Applet,ScrollPane, Window,Dialog,FileDialog,Frame,Label,List,Scrollbar, TextArea,and TextField |
WindowEvent WindowListener addWindowListener() removeWindowListener() | Window and its derivatives, including Dialog,FileDialog,and Frame |
ItemEvent ItemListener addItemListener() removeItemListener() | Checkbox,CheckboxMenuItem,Choice,List,and anything that implements the ItemSelectable interface |
TextEvent TextListener addTextListener() removeTextListener() | Anything derived from TextComponent,including TextArea and TextField |
比起上面这张表格下面的表格更符合OO作风:
Componenttype | Event supported by this component |
Adjustable | AdjustmentEvent |
Applet | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Button | ActionEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Canvas | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Checkbox | ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
CheckboxMenuItem | ActionEvent, ItemEvent |
Choice | ItemEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Component | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Container | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Dialog | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
FileDialog | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Frame | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Label | FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
List | ActionEvent, FocusEvent, KeyEvent, MouseEvent, ItemEvent, ComponentEvent |
Menu | ActionEvent |
MenuItem | ActionEvent |
Panel | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
PopupMenu | ActionEvent |
Scrollbar | AdjustmentEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
ScrollPane | ContainerEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextArea | TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextComponent | TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
TextField | ActionEvent, TextEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
Window | ContainerEvent, WindowEvent, FocusEvent, KeyEvent, MouseEvent, ComponentEvent |
这之后是各个监听器的响应函数接口:
Listener interface/adapter | Methods in interface |
ActionListener | actionPerformed(ActionEvent) |
AdjustmentListener | adjustmentValueChanged(AdjustmentEvent) |
ComponentListener ComponentAdapter | componentHidden(ComponentEvent) componentShown(ComponentEvent) componentMoved(ComponentEvent) componentResized(ComponentEvent) |
ContainerListener ContainerAdapter | componentAdded(ContainerEvent) componentRemoved(ContainerEvent) |
FocusListener FocusAdapter | focusGained(FocusEvent) focusLost(FocusEvent) |
KeyListener KeyAdapter | keyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) |
MouseListener MouseAdapter | mouseClicked(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mousePressed(MouseEvent) mouseReleased(MouseEvent) |
MouseMotionListener MouseMotionAdapter | mouseDragged(MouseEvent) mouseMoved(MouseEvent) |
WindowListener WindowAdapter | windowOpened(WindowEvent) windowClosing(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) |
ItemListener | itemStateChanged(ItemEvent) |
TextListener | textValueChanged(TextEvent) |
掌握了这些以后消息机制以后其余的awt各个组件具体使用方法都能在API进行查询。如果在API里查询以后仍然没有弄懂使用方法的话可以参考书中作者给出的各个组件的示例。
但是读到书中13.18节,由于我完全没有使用过java beans所以不知道具体是个什么东西,书中说讲得内容也完全弄不懂。只能以后来弥补
最 后书中讨论的是Swing。个人认为这个是java中给出的最强大的编写GUI的库,他的组件比awt的组件性能强大(比如说button,Swing的 button能提供使用图片代替按钮原来造型等等),而且对于学习了awt的用户来说,Swing的上手绘非常简单,因为Swing里面的组件这是在 awt的组件前面加了个“J”而已(在awt中是Button,在Swing中就是JButton),同时这些组件基本的使用方法完全一样。而且 Swing不仅仅只是在awt的组件上进行了扩展,它还提供了其他的新组件(比如:例如JColorChooser, JFileChooser, JPasswordField, JHTMLPane(完成简单的HTML 格式化和显示)以及JTextPane(一个支持格式化,字处理和图像的文字编辑器)。JToolbar API 提供的可拖动的浮动工具条等等。)这些新组件在Think in java中并未示例或者介绍,具体要学习的话,作者建议我们去找关于Swing学习的专业书籍。但是基本组件的介绍还是有的,而且每个组件均有示例,可以作为初学者编写GUI程序的参照。