天天看点

黑马程序员—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. }  

继续阅读