天天看點

黑馬程式員—GUI

GUI:Graphical User Interface 圖形使用者接口

CLI:Command Uesr Interface 指令行使用者接口,比如常見的Dos指令行操作

AWT:Abstract Window ToolKit  抽象視窗工具包

Swing:在AWT的基礎上建立的一套圖形界面系統

GUI繼承關系圖

黑馬程式員—GUI

[html]  view plain copy

  1. package Base;  
  2. import java.awt.Button;  
  3. import java.awt.FlowLayout;  
  4. import java.awt.Frame;  
  5. import java.awt.TextArea;  
  6. import java.awt.TextField;  
  7. import java.awt.event.ActionEvent;  
  8. import java.awt.event.ActionListener;  
  9. import java.awt.event.WindowAdapter;  
  10. import java.awt.event.WindowEvent;  
  11. public class MyWindowDemo   
  12. {  
  13.     //定義成員變量  
  14.     private Frame f;//視窗  
  15.     private TextField tf;//文本行  
  16.     private Button zhuandaoButton,exitButton;//按鈕  
  17.     private TextArea ta;//文本區域  
  18.     MyWindowDemo()  
  19.     {  
  20.         init();  
  21.     }  
  22.     public void init()  
  23.     {  
  24.         //頂一個視窗  
  25.         f = new Frame("my window");  
  26.         //對frame進行基本設定  
  27.         //長寬高低  
  28.         f.setBounds(300,100,500,300);  
  29.         //設定布局管理器  
  30.         f.setLayout(new FlowLayout());  
  31.         tf=new TextField(50);  
  32.         zhuandaoButton = new Button("轉到");  
  33.         exitButton = new Button("退出");  
  34.         ta = new TextArea(10,60);  
  35.         //将組建添加到f中  
  36.         f.add(tf);  
  37.         f.add(zhuandaoButton);  
  38.         f.add(exitButton);  
  39.         f.add(ta);  
  40.         //加載窗體上的事件  
  41.         myEvent();  
  42.         //顯示窗體  
  43.         f.setVisible(true);  
  44.     }  
  45.     private void myEvent()  
  46.     {  
  47.         //定義事件,點選exitButton,就退出  
  48.         exitButton.addActionListener(new ActionListener()  
  49.         {  
  50.             public void actionPerformed(ActionEvent e)  
  51.             {  
  52.                 System.exit(0);  
  53.             }  
  54.         });  
  55.         //點選zhuandaoButton按鈕,就會将文本行中的資料添加到文本區域中  
  56.         zhuandaoButton.addActionListener(new ActionListener()  
  57.         {  
  58.             public void actionPerformed(ActionEvent e)  
  59.             {  
  60.                 //tf.getText("");  
  61.                 String text = tf.getText();  
  62.                 //System.out.println(text);  
  63.                 ta.append(text);  
  64.                 ta.append("");  
  65.             }  
  66.         });  
  67.         //關閉事件  
  68.         f.addWindowListener(new WindowAdapter()  
  69.         {  
  70.             public void windowClosing(WindowEvent e)  
  71.             {  
  72.                 System.exit(0);  
  73.             }  
  74.         });  
  75.     }  
  76.     public static void main(String[] args)   
  77.     {  
  78.         new MyWindowDemo();  
  79.     }  
  80. }  

繼續閱讀