JAVA 私塾第十、十一章笔记整理

luodis 发布于 2011/02/19 09:13
阅读 80
收藏 2

解读下一代网络:算力网络正从理想照进现实!>>>

JAVA 私塾第十、十一章笔记整理
 
第十章 AWT
 
    AWT(Abstract Window Toolkit)用于Java Application 的GUI(Graphical User Interface图形用户界面)编程。
    AWT所涉及的类一般在java.awt包及其子包中。
   【此处有图片,可以到JAVA 私塾官网下载完整笔记:www.javass.cn
 
    布局管理器 
        FlowLayout  (Panel和Applets的缺省布局管理器)
        对组件逐行定位,行内从左到右,一行排满后换行。默认的对齐方式是居中。
        BorderLayout   (Window, Dialog和Frame的缺省布局管理器)
        将整个容器的布局划分成东西南北中五部分,组件只能被添加到指定区域。每个区域只能加入一个组件,如加入多个,则先前的会被覆盖。
        GridLayout
        将空间划分成规则的矩形网格,每个单元区域大小相等,组件被添加到每个单元各种,先从左到右满一行后换行,再从上到下。
        CardLayout
        GridBagLayout
 
第十一章Swing和GUI事件处理
 
    GUI事件监听器接口:
 
[table=50%][tr][td]事件类型 [/td][td]相应监听器接口[/td][td]监听器接口中的方法[/td][/tr]
[tr][td]Action[/td][td]ActionListener[/td][td]actionPerformed(ActionEvent)[/td][/tr]
[tr][td]Item[/td][td]ItemListener[/td][td]itemStateChanged(ItemEvent)[/td][/tr]
[tr][td]Mouse[/td][td]MouseListener[/td][td]mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)[/td][/tr]
[tr][td]Mouse Motion[/td][td]MouseMotionListener[/td][td]mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
[/td][/tr]
[tr][td]Key[/td][td]KeyListener[/td][td]keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
[/td][/tr]
[tr][td]Focus[/td][td]FocusListener[/td][td]focusGained(FocusEvent)
focusLost(FocusEvent)
[/td][/tr]
[tr][td]Adjustment[/td][td]AdjustmentListener[/td][td]adjustmentValueChanged(AdjustmentEvent)[/td][/tr]
[tr][td]Component[/td][td]ComponentListener[/td][td]componentMoved(ComponentEvent)
componentHidden (ComponentEvent)
componentResized(ComponentEvent)
componentShown(ComponentEvent)[/td][/tr]
[tr][td]Window[/td][td]WindowListener[/td][td]windowClosing(WindowEvent)
windowOpened(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
[/td][/tr]
[tr][td]Container[/td][td]ContainerListener[/td][td]componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
[/td][/tr]
[tr][td]Text[/td][td]TextListener[/td][td]textValueChanged(TextEvent)[/td][/tr][/table]
 
    事件适配器:
[code]
public abstract class WindowAdapter implements WindowListener {
   public void windowOpened(WindowEvent e) {}
   public void windowClosing(WindowEvent e) {}
   public void windowClosed(WindowEvent e) {}
   public void windowIconified(WindowEvent e) {}
   public void windowDeiconified(WindowEvent e) {}
   public void windowActivated(WindowEvent e) {}
   public void windowDeactivated(WindowEvent e) {}
}[/code]
 
内部类
[code]
import java.awt.*;
import java.awt.event.*;
public class Test1{
   Frame f = new Frame("Java Gui");
   public Test1(){
      MyInner m = new MyInner();
      f.addWindowListener(m);
      f.setSize(150,150);
      f.setVisible(true);
   }
   public static void main(String args[]){
      new Test1();
   }
   class MyInner extends WindowAdapter{
      public void windowClosing(WindowEvent e){
         System.exit(1);
      }
   }
}[/code]
 
    匿名内部类
[code]
import java.awt.*;
import java.awt.event.*;
public class Test2{
   Frame f = new Frame("Java Gui");
   public Test2(){
      f.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
            System.exit(1);
         }
      });
      f.setSize(150,150);
      f.setVisible(true);
   }
   public static void main(String args[]){
      new Test2();
   }
}[/code]
 
    什么是双缓冲?
       在后台进行界面的更新,然后在前台进行界面交换
       功能:双缓冲可以改善一个被频繁改变的组件的外观
 
    *Swing和AWT的区别?
       Swing提供了更完整的组件,引入了许多新的特性和能力。Swing API是围绕着实现           
       AWT 各个部分的API 构筑的。
       AWT 采用了与特定平台相关的实现,而绝大多数Swing 组件却不是这样做的,因此Swing 的外观和感觉是可客户化和可插的。

加载中
返回顶部
顶部