java基础<图形用户界面GUI>

GUI概述

一、概述
        
 

       



Component:构件、组件                                                            Button:按钮
Container:容器(能添加组件)    Label:标签(封装文字)
Window:窗口   Checkbox:复选框
Frame:框架   TextCompent:文本组件
Dialog:对话框   TextArea:
FileDialog:文件对话框   TextFiled:

GUI布局

一、布局管理器分类
1.流式布局管理器 FlowLayout

2.边界式布局管理器 BorderLayout


如果不指定组件的方向,就会如右图所示,最大面积填充这个窗体(因为默认的情况就是居中),如果再添加组件那么就会继续覆盖。
3.网格布局管理器 GridLayout
例子:计算器

4.网格包布局管理器 GridBagLayout
例子:计算器
5.卡片布局管理器  CardLayout


6.坐标式布局 
JBulider,eclipse+图形化界面

****如果遇到混合式布局的情况,先需要用panel面板来存放布局****

Frame

一、概述

Frame 是带有标题和边框的顶层窗口。 

窗体的默认布局为 BorderLayout。 

二、代码
  1. /* 
  2. 创建图形化界面的思路: 
  3. 1.创建frame窗体。 
  4. 2.对窗体进行基本设置 
  5. 比如大小,位置,布局。 
  6. 3.定义组件。 
  7. 4.将组件通过窗体的add方法添加到界面中 
  8. 5.让窗体显示,通过setVisible(true)方法。 
  9.  
  10. */  
  11.   
  12. import java.awt.*;  
  13. class  AwtDemo  
  14. {  
  15.     public static void main(String[] args)   
  16.     {     
  17.         Frame f=new Frame("my awt");//默认的布局为——边界式布局。  
  18.         f.setSize(500,400);//用来设置窗体的长和宽,(x,y)坐标轴的形式  
  19.         f.setLocation(300,200);//用来设置窗体左上角的顶点距离左、上的距离。  
  20.         f.setLayout(new FlowLayout());//将该窗体的布局改为流式布局         
  21.         Button b=new Button("我是一个按钮");//创建一个按钮对象  
  22.         f.add(b);//向窗体f内添加一个按钮  
  23.         f.setVisible(true);//设置f这个窗体为可见,如果不设置则Frame不显示  
  24.         System.out.println("Hello World!");  
  25.     }  
  26. }  

事件监听机制

一、概述



事件监听机制的特点:
1.事件源
2.事件
3.监听器
4.事件处理
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件
监听器:将可以触发某一个事件的动作都已经封装到了监听器中。
以上三者都已经定义好了,直接获取其对象来用就可以了。

我们要做的事情就是对产生的动作进行处理。

窗体事件

一、窗体事件实现
Window类——> addWindowListener(WindowListener l) 方法——>WindowListener接口——>WindowAdapter抽象类
有两种方法实现事件监听
1.匿名内部类
  1. f.addWindowListener(new WindowAdapter()  
  2. {  
  3.     public void windowClosing(WindowEvent e)  
  4.     {  
  5.     System.exit(0);           
  6.     }  
  7.       
  8.       
  9. });  

2.继承WindowAdapter抽象类
  1. class MyWin extends WindowAdapter  
  2. {  
  3.     public void windowClosing(WindowEvent e)  
  4.     {  
  5.         //System.out.println("window is closing..."+e.toString);  
  6.         System.exit(0);  
  7.     }  
  8.   
  9. }  

二、完整代码
  1. import java.awt.*;  
  2. import java.awt.event.*;//事件监听需要导入此包  
  3. class  AwtDemo  
  4. {  
  5.     public static void main(String[] args)   
  6.     {     
  7.         Frame f=new Frame("my awt");//默认的布局为——边界式布局。  
  8.         f.setSize(500,400);//用来设置窗体的长和宽,(x,y)坐标轴的形式  
  9.         f.setLocation(300,200);//用来设置窗体左上角的顶点距离左、上的距离。  
  10.         f.setLayout(new FlowLayout());//将该窗体的布局改为流式布局         
  11.         Button b=new Button("我是一个按钮");//创建一个按钮对象  
  12.         f.add(b);//向窗体f内添加一个按钮  
  13.   
  14.   
  15.         //f.addWindowListener(new MyWin());  
  16.   
  17.         f.addWindowListener(new WindowAdapter()  
  18.         {  
  19.             public void windowClosing(WindowEvent e)  
  20.             {  
  21.             System.exit(0);           
  22.             }  
  23.               
  24.               
  25.         });  
  26.   
  27.   
  28.         f.setVisible(true);//设置f这个窗体为可见,如果不设置则Frame不显示  
  29.         System.out.println("Hello World!");  
  30.     }  
  31. }  
  32.   
  33.   
  34.   
  35.   
  36. class MyWin extends WindowAdapter  
  37. {  
  38.     public void windowClosing(WindowEvent e)  
  39.     {  
  40.         //System.out.println("window is closing..."+e.toString);  
  41.         System.exit(0);  
  42.     }  
  43.   
  44. }  

Action事件

一、概述
Button类——> addActionListener(ActionListener l)方法——>ActionListener接口
由于ActionListener内的方法少于三个,所以它没有adapter适配器。
二、代码
  1. b.addActionListener(new ActionListener()  
  2. {  
  3.     public void actionPerformed(ActionEvent e)  
  4.     {  
  5.         System.exit(0);           
  6.     }  
  7.   
  8.   
  9. });  

共性事件——鼠标事件和键盘事件

一、概述
1.Componet类——> addMouseListener(MouseListener l)——>MouseLister接口——>MouseAdapter抽象类
2.Component类——>addKeyListener(KeyListener l)——>KeyListener接口——>KeyAdapter抽象类
二、代码
1.鼠标事件代码
  1. but.addMouseLister(new MouseAdapter()  
  2. {  
  3.     int count=1;  
  4.     public void mouseEntered(MouseEvent e)//鼠标进入组件事件  
  5.     {  
  6.         System.out.println("鼠标进入Button组件"+count++);           
  7.     }  
  8.   
  9.     public void mouseClicked(MouseEvent e)//鼠标点击事件。如果有action事件存在,那么也会先执行鼠标事件。  
  10.     {     
  11.         if(e.getClickCount()==2)//用MouseEvent事件e来监听点击了几次  
  12.         System.out.println("鼠标点击了Button组件"+count++);  
  13.     }         
  14. });  
2.键盘事件代码
  1. but.addKeyListener(new KeyAdapter()//键盘监听  
  2. {  
  3.     public void keyPressed(KeyEvent e)//由键盘事件对象来获取键盘按下的键  
  4.     {  
  5.         //e.getKeyChar();获取按下了那些键  
  6.         //e.getKeyCode();获取按下键的“码”  
  7.         //KeyEvent.getKeyText();返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。  
  8.         if(e.getKeyCode()==27/*或者把27换成KeyEvent.VK_ESCAPE*/)//如果but是当前事件源,按下ESC则会退出  
  9.         {  
  10.             System.exit(0)  
  11.         }                 
  12.     }  
  13.   
  14. });  
3.完整代码
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3.   
  4. class MouseAndKeyEvent   
  5. {  
  6.     private Frame f;  
  7.     private Button but;  
  8.     private TextField tf;  
  9.   
  10.     MouseAndKeyEvent()  
  11.     {  
  12.         init();  
  13.   
  14.     }  
  15.   
  16.     public void init()  
  17.     {  
  18.         f=new Frame("my frame");  
  19.         f.setBounds(300,100,600,500);  
  20.         f.setLayout(new FlowLayout());  
  21.   
  22.         tf=new TextField(20);  
  23.         f.add(tf);  
  24.         but =new Button("my button");  
  25.   
  26.         f.add(but);  
  27.           
  28.         myEvent();  
  29.         f.setVisible(true);  
  30.     }  
  31.   
  32.     private void myEvent()  
  33.     {  
  34.         f.addWindowListener(new WindowAdapter()  
  35.         {  
  36.             public void windowClosing(WindowEvent e)  
  37.             {  
  38.                 System.exit(0);  
  39.             }  
  40.         });  
  41.           
  42.         but.addActionListener(new ActionListener()  
  43.         {  
  44.             public void actionPerformed(ActionEvent e)//鼠标和键盘都可以让Action触发。  
  45.             {  
  46.                 System.out.println("action OK");          
  47.             }  
  48.           
  49.         });  
  50.   
  51.   
  52.         but.addMouseListener(new MouseAdapter()  
  53.         {  
  54.             int count=1;  
  55.             public void mouseEntered(MouseEvent e)//鼠标进入组件事件  
  56.             {  
  57.                 System.out.println("鼠标进入Button组件"+count++);           
  58.             }  
  59.   
  60.             public void mouseClicked(MouseEvent e)//鼠标点击事件。如果有action事件存在,那么也会先执行鼠标事件。  
  61.             {     
  62.                 if(e.getClickCount()==2)//用MouseEvent事件e来监听点击了几次  
  63.                 System.out.println("鼠标点击了Button组件"+count++);  
  64.             }         
  65.         });  
  66.   
  67.         but.addKeyListener(new KeyAdapter()//键盘监听  
  68.         {  
  69.             public void keyPressed(KeyEvent e)//由键盘事件对象来获取键盘按下的键  
  70.             {  
  71.                 //e.getKeyChar();获取按下了那些键  
  72.                 //e.getKeyCode();获取按下键的“码”  
  73.                 //KeyEvent.getKeyText();返回描述 keyCode 的 String,如 "HOME"、"F1" 或 "A"。  
  74.                 if(e.getKeyCode()==27/*或者把27换成KeyEvent.VK_ESCAPE*/)//如果but是当前事件源,按下ESC则会退出  
  75.                 {  
  76.                     System.exit(0);  
  77.                 }         
  78.                       
  79.                 if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//实现ctrl+回车的组合键功能,其中isControlDown()是InputEvent类  
  80.                 {                                                       //下继承过来的方法。  
  81.                     System.exit(0);  
  82.                 }  
  83.             }  
  84.           
  85.         });  
  86.           
  87.         tf.addKeyListener(new KeyAdapter()  
  88.         {  
  89.             public void keyPressed(KeyEvent e)  
  90.             {  
  91.                 if(!(e.getKeyCode()>KeyEvent.VK_0&&e.getKeyCode()<KeyEvent.VK_9))  
  92.                 {  
  93.                     System.out.println("您输入的字符非法");  
  94.                     e.consume();//简单来说,该方法是用来取消该事件的操作。  
  95.                 }  
  96.             }         
  97.           
  98.         });  
  99.     }  
  100.   
  101.   
  102.     public static void main(String[] args)   
  103.     {  
  104.         new MouseAndKeyEvent();  
  105.     }  
  106. }  

列出指定目录内容(带有对话框)

一、代码
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.io.*;  
  4. class DialogDirectoryDemo2   
  5. {  
  6.     private Frame f;  
  7.     private Button b;  
  8.     private TextField tf;  
  9.     private TextArea ta;  
  10.     private Dialog d;  
  11.     private Label l;  
  12.     private Button ok;  
  13.   
  14.       
  15.     DialogDirectoryDemo2()  
  16.     {  
  17.         init();  
  18.     }  
  19.   
  20.   
  21.     private void init()  
  22.     {  
  23.         f=new Frame("my frame");  
  24.         f.setBounds(300,200,500,600);  
  25.         f.setLayout(new FlowLayout());  
  26.         tf=new TextField(40);  
  27.         b=new Button("转到");  
  28.         ta=new TextArea(30,50);  
  29.   
  30.         d=new Dialog(f,"错误提示",false);  
  31.         d.setBounds(400,300,200,100);  
  32.         d.setLayout(new FlowLayout());  
  33.         l=new Label("您所输入的目录不存在,请重新输入",Label.CENTER);  
  34.         ok=new Button("确定");  
  35.         d.add(l);  
  36.         d.add(ok);  
  37.           
  38.         f.add(tf);  
  39.         f.add(b);  
  40.         f.add(ta);  
  41.   
  42.         DemoEvent();  
  43.         f.setVisible(true);  
  44.           
  45.     }  
  46.   
  47.     public void DemoEvent()  
  48.     {  
  49.     /*1*/f.addWindowListener(new WindowAdapter()//监听f的WindowEvent e  
  50.         {  
  51.             public void windowClosing(WindowEvent e)  
  52.             {  
  53.                 System.exit(0);           
  54.             }  
  55.           
  56.         });  
  57.           
  58.     /*2*/d.addWindowListener(new WindowAdapter()//监听d的WindowEvent e  
  59.         {  
  60.             public void windowClosing(WindowEvent e)  
  61.             {  
  62.                 d.setVisible(false);  
  63.             }  
  64.         });  
  65.   
  66.     /*3*/ok.addActionListener(new ActionListener()//监听ok的ActionEvent  
  67.         {  
  68.             public void actionPerformed(ActionEvent e)  
  69.             {  
  70.                 d.setVisible(false);  
  71.             }  
  72.         });  
  73.   
  74.   
  75.     /*4*/b.addActionListener(new ActionListener()//监听b的ActionEvent  
  76.         {  
  77.             public void actionPerformed(ActionEvent e)  
  78.             {  
  79.                     show();  
  80.             }  
  81.                       
  82.         });  
  83.               
  84.       
  85.     /*5*/tf.addActionListener(new ActionListener()//监听tf的ActionEvent  
  86.         {  
  87.             public void actionPerformed(ActionEvent e)  
  88.             {  
  89.                     show();  
  90.             }  
  91.                       
  92.         });  
  93.   
  94.     }  
  95.   
  96.   
  97.       
  98.     public void show() //将在TextField按下回车和点击Button做出的相同的事件响应进行封装  
  99.     {  
  100.                 String s=tf.getText();  
  101.                 File file=new File(s);  
  102.                 if(file.isDirectory()&&file.exists())  
  103.                 {     
  104.                     ta.setText("");  
  105.                     String[] list=file.list();  
  106.                     for(String str:list)  
  107.                     {  
  108.                         ta.append(str+"\r\n");                    
  109.                     }  
  110.                 }  
  111.                 else  
  112.                 {  
  113.                     l.setText("你输入的目录\""+s+"\"不存在");  
  114.                     d.setVisible(true);  
  115.                               
  116.                 }  
  117.                 tf.setText("");  
  118.     }  
  119.   
  120.   
  121.   
  122.     public static void main(String[] args)   
  123.     {  
  124.         new DialogDirectoryDemo2();  
  125.     }  
  126. }  

可以打开文件和保存文件的——GUI菜单

一、概述
菜单栏(MenuBar)——>菜单(Menu)——>菜单项(MenuItem)或者菜单(Menu,即子菜单)
二、代码
  1. import java.io.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class SaveAndOpenMenu   
  5. {  
  6.     private Frame f;  
  7.     private TextArea ta;  
  8.     private MenuBar mb;  
  9.     private Menu m;  
  10.     private MenuItem mi1;  
  11.     private MenuItem mi2;  
  12.     private MenuItem mi3;  
  13.     private File file;  
  14.   
  15.     SaveAndOpenMenu()  
  16.     {  
  17.         init();  
  18.     }  
  19.   
  20.     public void init()  
  21.     {  
  22.         f=new Frame("DEMO");  
  23.         f.setBounds(500,400,400,500);  
  24.         ta=new TextArea();  
  25.         mb=new MenuBar();  
  26.         m=new Menu("选项");  
  27.         mi1=new MenuItem("保存");  
  28.         mi2=new MenuItem("打开");  
  29.         mi3=new MenuItem("关闭");  
  30.         mb.add(m);  
  31.         m.add(mi1);  
  32.         m.add(mi2);  
  33.         m.add(mi3);  
  34.         f.setMenuBar(mb);  
  35.         f.add(ta);  
  36.         f.setVisible(true);  
  37.         event();  
  38.           
  39.     }  
  40.   
  41.     public void event()  
  42.     {  
  43.         f.addWindowListener(new WindowAdapter()  
  44.         {  
  45.             public void windowClosing(WindowEvent e)  
  46.             {  
  47.                 System.exit(0);  
  48.             }         
  49.         });  
  50.   
  51.         mi3.addActionListener(new ActionListener()  
  52.         {  
  53.             public void actionPerformed(ActionEvent e)  
  54.             {  
  55.                 System.exit(0);  
  56.             }  
  57.         });  
  58.   
  59.         mi1.addActionListener(new ActionListener()  
  60.         {  
  61.             public void actionPerformed(ActionEvent e)  
  62.             {  
  63.                 if(file==null)  
  64.                 {  
  65.                 FileDialog fd=new FileDialog(f,"保存文件",FileDialog.SAVE);  
  66.                 fd.setVisible(true);  
  67.                 String directory=fd.getDirectory();  
  68.                 String name=fd.getFile();  
  69.                 if(directory==null||name==null)  
  70.                     return;  
  71.                 file=new File(directory,name);  
  72.                 }  
  73.                 BufferedWriter bw=null;       
  74.                 try  
  75.                 {  
  76.                 String str=ta.getText();  
  77.                 bw=new BufferedWriter(new FileWriter(file));  
  78.                 bw.write(str);    
  79.                 bw.close();  
  80.                 }  
  81.                 catch (IOException ex)  
  82.                 {  
  83.                     throw new RuntimeException("文件保存出现问题");  
  84.                 }  
  85.             }         
  86.         });  
  87.           
  88.         mi2.addActionListener(new ActionListener()  
  89.         {  
  90.             public void actionPerformed(ActionEvent e)  
  91.             {  
  92.                 FileDialog fd=new FileDialog(f,"保存文件",FileDialog.LOAD);  
  93.                 fd.setVisible(true);  
  94.                 String directory=fd.getDirectory();  
  95.                 String name=fd.getFile();  
  96.                 if(directory==null||name==null)  
  97.                     return;  
  98.                 file=new File(directory,name);  
  99.                 ta.setText("");  
  100.                 BufferedReader br=null;  
  101.                 try  
  102.                 {  
  103.                     br=new BufferedReader(new FileReader(file));  
  104.                     String str=null;  
  105.                     while((str=br.readLine())!=null)  
  106.                     {  
  107.                         ta.append(str+"\r\n");  
  108.                     }  
  109.                     br.close();  
  110.                 }  
  111.                 catch (IOException ex)  
  112.                 {  
  113.                     throw new RuntimeException("文件打开出现问题");  
  114.                 }                         
  115.             }  
  116.                   
  117.         });  
  118.   
  119.   
  120.       
  121.         }  
  122.   
  123.   
  124.       
  125.       
  126.       
  127.       
  128.       
  129.       
  130.       
  131.     public static void main(String[] args)   
  132.     {  
  133.         new SaveAndOpenMenu();  
  134.     }  
  135. }  

JAR包双击执行

一、步骤
1.在dos环境下,.java所在文件的目录下执行以下代码
javac -d c:\ SavaAndOpenMenu.java
2.在c:\下执行以下代码
jar -cvf my.jar mymenu

但是执行以上代码会导致执行失败,原因是没有明确主函数的类。
所以需要进行以下操作
3.在c:\文件下创建一个随意命名的.txt文件,比如1.txt,并在1.txt文件中配置如下信息
Main-class: mymenu.SavaAndOpenMenu(并在信息配置结束后添加一个回车,表示该行信息配置已经到达结尾)
4.在c:\下执行以下代码
jar -cvfm my.jar 1.txt mymenu

经过以上操作即可实现JAR包双击执行。

(PS:以上步骤中的c:\文件自己指定,mymenu为类包。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值