黑马程序员——GUI

------- android培训java培训、期待与您交流! ---------- 

 GUI
Graphical User Interface(图形用户接口)。
用图形的方式,来显示计算机操作的界面,这样更方便更直观。

 

Java为GUI提供的对象都存在java.Awt抽象窗口工具包,需要调用本地系统方法实现功能。属重量级控件 

javax.Swing基于Awt基础,建立一套图形界面系统,提供更多组件,完全由java实现。增强移植性,属轻量级控件

 Container:为容器,是一个特殊组件,该组件中可以通过add方法添加其他组件进来

 

常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器。
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 简单的窗体创建过程:  
  2.   
  3. Frame  f = new Frame(“my window”);  
  4.   
  5. f.setLayout(new FlowLayout());    //设置布局管理器  
  6.   
  7. f.setBounds(400,400,600,500);//设置窗体大小及出现位置  
  8.   
  9. //f.setSize(600,500);    //设置窗体大小  
  10. //f.setLocation(400,400);    //设置窗体出现在屏幕的位置  
  11.   
  12. f.setIconImage(Toolkit.getDefaultToolkit().createImage("qq.png"));  
  13.   
  14. Button button = new Button("我是一个按钮");  
  15.   
  16. f.add(button);  
  17. //显示窗体  
  18. f.setVisible(true);  

 

 

事件监听机制

1,确定事件源(容器或组件)

2,通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。
该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。一般用匿名内部类来表示。
3,在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。

4,事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)


 

 窗口事件关闭窗体

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //事件源是窗体,把监听器注册到事件源上  
  2.   
  3. //事件对象传递给监听器  
  4.   
  5. f.addWindowListener(new WindowAdapter() {  
  6.   
  7.         public void windowClosing(WindowEvent e) {  
  8.         System.out.println("关闭");  
  9.          //退出虚拟机,关闭窗口  
  10.         System.exit(0);  
  11.   
  12.     }  
  13.   
  14. });  


 

按钮关闭窗体

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. but.addActionListener(new ActionListener(){  
  2.     public void actionPerformed(ActionEvent e){  
  3.         System.exit(0);  
  4.     }  
  5.     });  


 

 鼠标监听器(MouseListener)

 void mouseClicked(MouseEvent e) 
          鼠标按键在组件上单击(按下并释放)时调用。 
 void mouseEntered(MouseEvent e) 
          鼠标进入到组件上时调用。 
 void mouseExited(MouseEvent e) 
          鼠标离开组件时调用。 
 void mousePressed(MouseEvent e) 
          鼠标按键在组件上按下时调用。 
 void mouseReleased(MouseEvent e) 
          鼠标按钮在组件上释放时调用。

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. but.addMouseListener(new MouseAdapter(){  
  2.     private int count = 1;  
  3.     private int clickCount = 1;  
  4.   
  5.     public void mouseEntered(MouseEvent e){  
  6.         System.out.println("鼠标进入"+count++);  
  7.     }  
  8.   
  9.     public void mouseClicked(MouseEvent e){  
  10.         if(e.getClickCount()==2)  
  11.             System.out.println("双击进入"+clickCount++);  
  12.     }  
  13. });  


 键盘按下事件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. but.addKeyListener(new KeyAdapter(){  
  2.     public void keyPressed(KeyEvent e){  
  3.         //Control和Control按下  
  4.         if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_Control)  
  5.             System.out.println("ctrl+enter is run");  
  6.             //根据码获取所按的键  
  7.             //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());  
  8.     }  
  9. });  


 

文本框键盘监听事件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. tf.addKeyListener(new KeyAdapter(){  
  2.     public void keyPressed(KeyEvent e){  
  3.     int code = e.getKeyCode();  
  4.     if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){  
  5.         System.out.println(KeyEvent.getKeyText(code)+"....非法");  
  6.         e.consume();//屏蔽  
  7.         }                 
  8.     }  
  9. });  


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //列出指定目录内容  
  2. //错误提示对话框  
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5. import java.io.*;  
  6.   
  7. class MyWindowDemo{  
  8.     private Frame f;  
  9.     private TextField tf;  
  10.     private Button but;  
  11.     private TextArea ta;  
  12.     private Dialog d;//对话框  
  13.     private Label lab;//文字  
  14.     private Button okBut;  
  15.   
  16.     MyWindowDemo(){  
  17.         init();  
  18.     }  
  19.   
  20.     public void init(){  
  21.         f = new Frame("my window");  
  22.         f.setBounds(400,200,600,400);  
  23.         f.setLayout(new FlowLayout());  
  24.   
  25.         tf= new TextField(30);  
  26.   
  27.         but = new Button("转到");  
  28.   
  29.         ta = new TextArea(15,40);  
  30.         //不关闭无法操作其它  
  31.         d = new Dialog(f,"提示信息-self",true);  
  32.         d.setBounds(400,200,300,180);  
  33.         d.setLayout(new FlowLayout());  
  34.           
  35.         lab = new Label();  
  36.   
  37.         okBut = new Button("确定");  
  38.   
  39.         d.add(lab);  
  40.         d.add(okBut);  
  41.   
  42.         f.add(tf);  
  43.         f.add(but);  
  44.         f.add(ta);  
  45.           
  46.         myEvent();  
  47.       
  48.         f.setVisible(true);  
  49.     }  
  50.   
  51.     public void myEvent(){  
  52.         d.addWindowListener(new WindowAdapter(){  
  53.             public void windowClosing(WindowEvent e){  
  54.                 d.setVisible(false);  
  55.             }  
  56.         });  
  57.   
  58.         okBut.addActionListener(new ActionListener(){  
  59.             public void actionPerformed(ActionEvent e)  
  60.             {  
  61.                 d.setVisible(false);  
  62.             }  
  63.         });  
  64.   
  65.         tf.addKeyListener(new KeyAdapter(){  
  66.             public void keyPressed(KeyEvent e)  
  67.             {  
  68.                 //键盘事件-回车  
  69.                 if (e.getKeyCode()==KeyEvent.VK_ENTER)  
  70.                     showDir();  
  71.                   
  72.             }  
  73.         });  
  74.   
  75.         but.addActionListener(new ActionListener(){  
  76.             public void actionPerformed(ActionEvent e)  
  77.             {  
  78.                 showDir();  
  79.             }  
  80.         });  
  81.   
  82.         f.addWindowListener(new WindowAdapter(){  
  83.             public void windowClosing(WindowEvent e)  
  84.             {  
  85.                 System.exit(0);  
  86.             }  
  87.         });  
  88.     }  
  89.   
  90.     private void showDir(){  
  91.         String dirPath = tf.getText();//指定的目录  
  92.   
  93.         File dir = new File(dirPath);  
  94.   
  95.         if(dir.exists() && dir.isDirectory()){//存在并且是目录  
  96.             ta.setText("");//清空  
  97.             String[] names = dir.list();  
  98.             for (String name : names ){  
  99.                 ta.append(name+"\r\n");  
  100.             }  
  101.         }  
  102.             else{  
  103.                 String info = "输入的信息:"+dirPath+"是错误的,请重新输入";  
  104.                 lab.setText(info);  
  105.                 d.setVisible(true);           
  106.             }                 
  107.     }  
  108.           
  109.     public static void main(String[] args) {  
  110.         new MyWindowDemo();  
  111.     }  
  112. }  


 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //菜单应用:打开保存退出  
  2.   
  3.   
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. import java.io.*;  
  7.   
  8. class  MyMenuText{  
  9.   
  10.     private Frame f;  
  11.     private MenuBar bar;//菜单栏  
  12.     private Menu fileMenu ;//子菜单  
  13.     private MenuItem closeItem,openItem,saveItem;//条目  
  14.   
  15.     private FileDialog openDia,saveDia;  
  16.   
  17.     private TextArea ta;  
  18.   
  19.     private File file;  
  20.   
  21.     MyMenuText(){  
  22.         init();  
  23.     }  
  24.       
  25.     public void init(){  
  26.         f = new Frame("my window");  
  27.         f.setBounds(400,200,600,400);         
  28.   
  29.         bar = new MenuBar();  
  30.   
  31.         ta= new TextArea();  
  32.   
  33.         fileMenu = new Menu("文件");  
  34.   
  35.         closeItem = new MenuItem("退出");  
  36.         saveItem = new MenuItem("保存");  
  37.         openItem = new MenuItem("打开");  
  38.   
  39.         fileMenu.add(openItem);  
  40.         fileMenu.add(saveItem);  
  41.         fileMenu.add(closeItem);  
  42.           
  43.         bar.add(fileMenu);  
  44.   
  45.         f.setMenuBar(bar);  
  46.           
  47.   
  48.         openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);//打开  
  49.         saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);//保存  
  50.           
  51.         f.add(ta);  
  52.         myEvent();  
  53.   
  54.         f.setVisible(true);  
  55.     }  
  56.   
  57.     public void myEvent(){  
  58.         //保存条目事件  
  59.         saveItem.addActionListener(new ActionListener(){  
  60.             public void actionPerformed(ActionEvent e){  
  61.                 //文件不存在,保存文件  
  62.                 if(file==null){  
  63.                 saveDia.setVisible(true);  
  64.                   
  65.                 String dirPath = saveDia.getDirectory();  
  66.                 String fileName = saveDia.getFile();  
  67.                   
  68.                 if(dirPath==null || fileName==null)  
  69.                     return ;  
  70.                   
  71.                  file = new File(dirPath,fileName);  
  72.                 }  
  73.                 //存在则修改  
  74.                 try{  
  75.                     BufferedWriter bufw = new BufferedWriter(new FileWriter(file));  
  76.                     //获取文本区的内容  
  77.                     String text = ta.getText();  
  78.   
  79.                     bufw.write(text);  
  80.                       
  81.                     bufw.close();  
  82.                 }  
  83.                 catch (IOException ex){  
  84.                     throw new RuntimeException();  
  85.                 }  
  86.             }  
  87.         });  
  88.   
  89.         //打开条目事件  
  90.         openItem.addActionListener(new ActionListener(){  
  91.             public void actionPerformed(ActionEvent e){  
  92.                 openDia.setVisible(true);  
  93.                 //获得路径  
  94.                 String dirPath = openDia.getDirectory();  
  95.                 //获得文件名  
  96.                 String fileName = openDia.getFile();  
  97.                   
  98.                 if(dirPath==null || fileName==null)  
  99.                     return ;  
  100.                 //打开之前先清空  
  101.                 ta.setText("");  
  102.                 file = new File(dirPath,fileName);  
  103.                   
  104.                 try{  
  105.                     BufferedReader bufr = new BufferedReader(new FileReader(file));  
  106.   
  107.                     String line = null;  
  108.   
  109.                     while ((line=bufr.readLine())!=null){  
  110.                         ta.append(line+"\r\n");  
  111.                     }  
  112.                     bufr.close();  
  113.                 }  
  114.                 catch (IOException ex){  
  115.                     throw new RuntimeException("读取失败");  
  116.                 }  
  117.                   
  118.             }  
  119.         });  
  120.   
  121.         closeItem.addActionListener(new ActionListener(){  
  122.             public void actionPerformed(ActionEvent e)  
  123.             {  
  124.                 System.exit(0);  
  125.             }  
  126.         });  
  127.   
  128.         f.addWindowListener(new WindowAdapter(){  
  129.             public void windowClosing(WindowEvent e){  
  130.                 System.exit(0);  
  131.             }  
  132.         });  
  133.     }  
  134.   
  135.     public static void main(String[] args) {  
  136.         new MyMenuText();  
  137.     }  
  138. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值